嗨,我尝试使用以下代码更新产品价格。但是由于某种原因,它显示错误。这是文档.请检查此 .
$storeId = storeid;
$productId = myproductid;
$myToken = mytoken;
$dataRAW = json_encode( array( 'price' => 80 ), JSON_FORCE_OBJECT );
$dataToPut = $dataRAW;
$dataRAW = http_build_query($dataRAW);
$context = [
'http' => [
'method' => 'PUT',
'header' => "Authorization: apikeystringrn" . "Content-Length: ".sizeof($dataToPut)."rn" . "Content-Type: application/jsonrn",
'content' => $dataToPut
]
];
$context = stream_context_create($context);
$url = "https://app.ecwid.com/api/v3/".urlencode($storeId)."/products/".urlencode($productId)."?token=".$myToken;
$dataToPut = json_encode($dataToPut);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: app.ecwid.com','Content-Type: application/json;charset=utf-8','Cache-Control: no-cache'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataToPut);
// Make the REST call, returning the result
$response = curl_exec($curl);
echo $response;
if (!$response) {
echo("Connection Failure: ".curl_error($curl));
die();
}
curl_close($curl);
我在我的本地主机中运行此代码。 http://localhost/ecwid/code.php
警告:http_build_query((:参数 1 应为数组或对象。在第 7 行的 C:\xampp\htdocs\ecwid\code.php 中给出的值不正确
警告:sizeof((:参数必须是在第 11 行的 C:\xampp\htdocs\ecwid\code.php 中实现 Countable 的数组或对象
连接失败:设置证书验证位置时出错:CA文件:C:\xampp\apache\bin\curl-ca-bundle.crt CApath:无
您正在http_build_query
中传递JSON
数据,因此发生了,http_build_query只接受数组参数并转换为查询字符串。您可以从以下示例中获取参考。
$dataRAW = ['price' => 80];
$dataRAW = http_build_query($dataRAW);