使用 PHP CURL 通过 REST API 更新 WooCommerce 产品

克里斯

似乎 $data 不正确。我可以列出产品并获得整个产品 JSON 作为响应。但是 WooCommerce 中的产品不会改变价格。当我通过 curl 命令行做同样的事情时,更新正在工作。
参考:https : //woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product

我究竟做错了什么?

<?php

$url = "https://wooexample.com/wp-json/wc/v3/products/455";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/json",
   "Authorization: Basic " . base64_encode ( 'ck_33fbff7b90dcddba9bd4cbedaeda6b2fa3:cs_4156da18fc357b288e42a7e7b75fa6682b' ),
);
// print_r($headers);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = [
    'regular_price' => '24',
    'price' => '24'
];

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
print_r(json_decode($resp));
拉杰夫·辛格

Ty 这个,我已经检查过了,它对我来说完美无缺。

<?php

$url = "https://localhost/wordpress/wp-json/wc/v3/products/455";

$consumer_key = 'your consumer key put here..';
$consumer_secret = 'your consumer secre put here..';

$headers = array(
    'Authorization' => 'Basic ' . base64_encode($consumer_key.':'.$consumer_secret )
);
$data = array(
    'regular_price' => '24',
    'price' => '24'
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
$resp = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
curl_close($curl);
print_r(json_decode($resp));

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过Rest API使用html添加WooCommerce产品内容

通过 REST API(清理缓存)更新产品库存数量后触发的 Woocommerce 挂钩

WooCommerce Rest API:使用图像创建产品

WooCommerce REST API通过ID获取多个产品

Woocommerce REST API更新产品类别

如何使用 PHP 通过 cURL 向 REST API 发送 HTTP GET 请求,并使用 cURL 从 REST API 调用方法

WooCommerce更新PHP中的产品

使用REST API从Woocommerce获取所有产品

使用woocommerce rest api添加产品太慢

使用自定义Wordpress API端点更新WooCommerce产品

使用php以编程方式更新*实际* woocommerce产品价格?

如何使用REST API为WooCommerce产品指定产品图片排序顺序?

使用cURL通过jQuery Ajax将数据作为JSON发送到PHP Rest API

通过Woocommerce Rest Api创建具有自定义字段的产品

如何通过woocommerce提供的rest api将产品插入购物车?

更新产品图片 WooCommerce API

基于php更新产品的woocommerce价格

Shopify Api使用PHP更新产品详细信息

使用cUrl通过其REST API配置Keycloak

Woocommerce Rest API(PHP)-将现有标签添加到现有产品吗?

WooCommerce REST API设置产品图像-WooCommerce.Net

为什么 woocommerce rest api 不起作用?我正在尝试通过 woocommerce rest api 以 json 形式访问产品详细信息

使用PHP的HP ALM REST API更新

Woocommerce - 使用 REST API 选择“单一产品变体”插件选择器

使用Woocommerce API检索可购买产品

WooCommerce:如何使用PHP返回产品重量?

PHP Rest Api多个参数如何使用curl发送

如何使用cURL在php中调用Base crm REST api?

通过PHP / Curl使用CloudFlare API返回错误