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?
Is 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