I need some help and guidance.
So I'm developing a website(custom theme) where the goal is to get data from a remote API. I want to be able to store the data in individual posts (one custom post type), and when someone adds or removes data to the remote API it should update the posts on the website.
The API I'm using is structured like this one:
I know how to get the data from it and decode the JSON etc.
$url = '';
$username = '*******************'; //This is not necessary for this API
$password = '****************'; //This is not necessary for this API
$headers = array( 'Authorization' => 'Basic ' . base64_encode( "$username:$password" ) ); //This is not necessary for this API
$request = wp_remote_get( $url, array( 'headers' => $headers ) );
if(is_wp_error($request)) {
return false;
}
$body = wp_remote_retrieve_body($request);
$data = json_decode($body);
Is it possible to create a create a custom post type for every product in this APIs and publish it on the website?
I need some help and guidance.
So I'm developing a website(custom theme) where the goal is to get data from a remote API. I want to be able to store the data in individual posts (one custom post type), and when someone adds or removes data to the remote API it should update the posts on the website.
The API I'm using is structured like this one:
https://pippinsplugins/edd-api/products
I know how to get the data from it and decode the JSON etc.
$url = 'https://pippinsplugins/edd-api/products';
$username = '*******************'; //This is not necessary for this API
$password = '****************'; //This is not necessary for this API
$headers = array( 'Authorization' => 'Basic ' . base64_encode( "$username:$password" ) ); //This is not necessary for this API
$request = wp_remote_get( $url, array( 'headers' => $headers ) );
if(is_wp_error($request)) {
return false;
}
$body = wp_remote_retrieve_body($request);
$data = json_decode($body);
Is it possible to create a create a custom post type for every product in this APIs and publish it on the website?
Share Improve this question edited Oct 26, 2018 at 7:40 hakanfilip asked Oct 25, 2018 at 23:42 hakanfiliphakanfilip 13 bronze badges 2- When you say a remote API can you be more specific? Is this remote API making requests to WordPress? Or is WordPress making requests to the remote API? Or is it a 3rd party? Or some kind of webhooks? It's not clear. Note that for questions here you need to ask a question specific enough that you can mark an answer as cannonically correct, not just what was most helpful, this isn't a discussion forum – Tom J Nowell ♦ Commented Oct 26, 2018 at 0:00
- @TomJNowell You're totally right, Thank you. After 8 hours of reading through articles on this subject and no success, I was pretty tired. Hope my edit makes things clearer! – hakanfilip Commented Oct 26, 2018 at 7:43
1 Answer
Reset to default 0Is it possible to create a create a custom post type for every product in this APIs and publish it on the website?
Yes, loop through the results, and for each result, check if the post already exists. If it doesn't use wp_insert_post
to create the post. Try doing it on a cron job so it happens at regular intervals
Alternatively, if it does not require a user/pass and authentication, you can fetch the data in the browser using javascript from the client side. That way it will always remain current, and you reduce the load on your server