Reputation: 67
I want to create posts (and update them) from an external API, which offers me the endpoints to connect to the JSON data, but I do not succeed to post any of this API data in my WordPress/WooCommerce.
I have tried to use some plugins to post JSON data (available in WordPress), but they did not work for me.
Using this endpoint "http://api.website.com/rest/catalog/product/1.json" in Postman with a GET request, it returns JSON data but when I paste the PHP code in my post, it does not return any data.
How can I post this JSON data that I fetch with Postman in my WooCommerce (as text, pictures/url, and tags)? And if any changes happen in the API server (i.e. Stock or Product Description), how can be that modified in an existing WP post (product)?
Any help or info will be appreciated since I am very new in the programming world! Thanks in advance :)
Upvotes: 3
Views: 3870
Reputation: 11861
$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v2/products/{PRODUCT ID}', array(
//'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
),
'body' => array(
'regular_price' => '100.30', // just update the product price
// but we can update several parameters at the same time
// more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
)
) );
$body = json_decode( $api_response['body'] );
//print_r( $body );
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
echo 'The product ' . $body->name . ' has been updated';
}
Please check this way
Upvotes: 3