邮政和从外部 API 更新 WooCommerce 中的 JSON 数据 [英] POST & UPDATE JSON data in WooCommerce from external API

查看:19
本文介绍了邮政和从外部 API 更新 WooCommerce 中的 JSON 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从外部 API 创建帖子(并更新它们),该 API 为我提供了连接到 JSON 数据的端点,但我没有成功在我的 WordPress/WooCommerce 中发布任何这些 API 数据.

我尝试使用一些插件来发布 JSON 数据(在 WordPress 中可用),但它们对我不起作用.

使用这个端点 "http://api.website.com/rest/catalog/product/1.json" 在带有 GET 请求的 Postman 中,它返回 JSON 数据,但是当我将 PHP 代码粘贴到我的帖子中时,它不返回任何数据.

如何在 WooCommerce 中发布使用 Postman 获取的 JSON 数据(作为文本、图片/网址和标签)?如果 API 服务器中发生任何更改(即库存或产品描述),如何在现有 WP 帖子(产品)中进行修改?

任何帮助或信息将不胜感激,因为我是编程世界的新手!提前致谢:)

解决方案

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v2/products/{PRODUCT ID}',大批(//'方法' =>'放','标题' =>大批('授权' =>'基本的 ' .base64_encode('KEY:SECRET')),'身体' =>大批('regular_price' =>'100.30',//只更新产品价格//但我们可以同时更新多个参数//更多参数 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' ) {回声产品".$body->name .' 已经更新';}

请检查这边

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 :)

解决方案

$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

这篇关于邮政和从外部 API 更新 WooCommerce 中的 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆