Error updating the details using rest api

2019-07-27 12:01发布

问题:

Hi i try to update a product price using the following code . But for some reason it showing error . This is documentation . Please check this .

$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);

I run this code in my localhost . http://localhost/ecwid/code.php

Warning: http_build_query(): Parameter 1 expected to be Array or Object. Incorrect value given in C:\xampp\htdocs\ecwid\code.php on line 7

Warning: sizeof(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\ecwid\code.php on line 11

Connection Failure: error setting certificate verify locations: CAfile: C:\xampp\apache\bin\curl-ca-bundle.crt CApath: none

回答1:

You are passing JSON data in http_build_query so it happened, http_build_query take only array params and convert into the query string. You can take reference from the following the example.

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


标签: php rest api curl