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

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

问题描述

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

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.

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

I have tried to use some plugins to post JSON data (available in WordPress), but they did not work for me.

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

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.

如何在WooCommerce中发布通过邮递员获取的JSON数据(作为文本,图片/URL和标签)?并且如果API服务器中发生了任何更改(例如,库存或产品说明),那么如何在现有的WP帖子(产品)中进行修改?

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';
}

请以这种方式检查

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

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