使用Rest API更新详细信息时出错

时间:2019-05-29 09:20:05

标签: php rest api curl

嗨,我尝试使用以下代码更新产品价格。但是由于某种原因它显示出错误。这是documentation。请检查一下。

$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: apikeystring\r\n" . "Content-Length: ".sizeof($dataToPut)."\r\n" . "Content-Type: application/json\r\n",
        '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的数组或对象

     

连接失败:错误设置证书验证位置:CAfile:C:\ xampp \ apache \ bin \ curl-ca-bundle.crt CApath:无

1 个答案:

答案 0 :(得分:1)

您正在JSON中传递http_build_query数据,因此它发生了,http_build_query仅采用数组参数并将其转换为查询字符串。您可以从以下示例中参考。

$dataRAW   = ['price' => 80];
$dataRAW   = http_build_query($dataRAW);