【问题标题】:PHP PATCH Product PayPal Catalog API Malformed request errorPHP PATCH 产品 PayPal 目录 API 格式错误的请求错误
【发布时间】:2021-01-14 03:12:00
【问题描述】:

我有一个让我发疯的问题。我正在尝试使用 PayPal Catalog API 更新产品。这是一个相当基本的 cURL 调用,使用下面的代码:

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

但它不断返回以下错误:

stdClass Object ( [name] => INVALID_REQUEST [message] => 请求格式不正确、语法不正确或违反架构。[debug_id] => 70cc270a1a2a1 [details] => Array ( [0] => stdClass Object ( [field] => / [location] => body [issue] => MALFORMED_REQUEST_JSON [description] => 请求 JSON 格式不正确。) ) [links] => Array ()

有人可以帮忙吗?谢谢!

【问题讨论】:

  • $ch 看起来怎么样?
  • 作为 echo 或 print_r() 它说“资源 id #7”

标签: php curl paypal paypal-sandbox


【解决方案1】:

根据Update product details 的文档,顶级项应该是带有[] 括号的数组

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

【讨论】:

  • 正确,但文档显示了直接 cURL 调用的示例,而我在 PHP 中使用 curl 对象,在这种情况下,POSTFIELDS 字符串被构造为形成 JSON 数组。这验证了jsonlint.com 可以,所以我不确定这是否是问题所在。我使用相同类型的字符串进行了类似的 POST 调用。
  • 规范规定请求正文必须是一个 array 补丁对象 developer.paypal.com/docs/api/catalog-products/v1/… -- 所以是的,这就是问题所在。
  • 请见谅!你是对的!除非我失明,否则我在任何地方都没有看到字符串中带有方括号的示例。非常感谢。
  • @Preston PHX 请帮我解决这个问题。 stackoverflow.com/questions/64081641/…
猜你喜欢
  • 2015-07-11
  • 2013-05-09
  • 2014-04-05
  • 2016-11-09
  • 2014-03-10
  • 2020-02-16
  • 2019-03-11
  • 2013-02-23
  • 2017-02-02
相关资源
最近更新 更多