Search code examples
phpcurlpaypalpaypal-sandbox

PHP PATCH Product PayPal Catalog API Malformed request error


I have an issue which is driving me nuts. I'm trying to update a Product using the PayPal Catalog API. It's a fairly basic cURL call using the code below:

$authorization="Authorization: Bearer ".$authorization;

curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/catalogs/products/RBW00002');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"op": "replace", "path": "/description", "value": "xxx"}');
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = $authorization;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

$editproduct= json_decode($result);

print_r($editproduct );
echo "<br><br>";

print_r($json);
echo "<br><br>";

unset($headers);
curl_close($ch);

but it keeps coming back with the following error:

stdClass Object ( [name] => INVALID_REQUEST [message] => Request is not well-formed, syntactically incorrect, or violates schema. [debug_id] => 70cc270a1a2a1 [details] => Array ( [0] => stdClass Object ( [field] => / [location] => body [issue] => MALFORMED_REQUEST_JSON [description] => The request JSON is not well formed. ) ) [links] => Array ( )

Can anyone help? Thanks!


Solution

  • From the documentation for Update product details, the top level item should be an array with [] brackets

    '[
      {
        "op": "replace",
        "path": "/description",
        "value": "Premium video streaming service"
      }
    ]'