php - curl 中的 paypal MALFORMED REQUEST 错误

标签 php json curl paypal paypal-sandbox

我在 paypal API 中面临 MALFORMED_REQUEST

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"intent\":\"authorize\",
\"payer\":{
    \"payment_method\":\"credit_card\",
    \"funding_instruments\":[{
        \"credit_card\":{
            \"number\":\"5555555555554444\",
            \"type\":\"mastercard\",
            \"expire_month\":07,
            \"expire_year\":22,
            \"cvv2\":123,
            \"first_name\":\"FName\",
            \"last_name\":\"Lname\",
            \"billing_address\":{
                \"line1\":\"address,\",
                \"city\":\"City\",
                \"state\":\"state\",
                \"postal_code\":\"postal_code\",
                \"country_code\":\"country_code\"
            }
        }
    }]
},
\"transactions\":[{
    \"amount\":{
        \"total\":\"10\",
        \"currency\":\"CAD\",
        \"details\":{
            \"subtotal\":\"10\",
            \"tax\":\"0\",
            \"shipping\":\"0\"
        }
    }
}]}");

curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer myAccessToken";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

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

curl_close($ch);

这里我用paypal调用curl进行授权

jsonlint.com说明这个json格式是可以的

但我仍然收到这样的回复,

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"9d7454a8637ae"}

没有弄清楚我哪里错了?有人知道吗?提前致谢!

最佳答案

您的问题是 "expire_month": 07 中的 07。我假设这个值是一个字符串。不确定您粘贴到 jsonlint 的内容,但这不是您问题中的 JSON。

如评论中所述,不要推出自己的 JSON。使用可用的工具

$data = [
    'intent' => 'authorize',
    'payer' => [
        'payment_method' => 'credit_card',
        'funding_instruments' => [[
            'credit_card' => [
                'number' => '5555555555554444',
                'type' => 'mastercard',
                'expire_month' => '07',
                'expire_year' => '22',
                'hopefully you get the idea' => 'by now'
            ]
        ]]
    ]
];

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

关于php - curl 中的 paypal MALFORMED REQUEST 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248066/

相关文章:

php - 将项目 ID 存储到 HTML 中

java - 在 Java/Android 中解析 UNIX 日期中的 JSON,无需任何库

php - 在 Zend_HTTP_Client 中跳过 SSL 检查

php - 如何从三个不同的表php中查询总计的SELECT SUM

php - 这个sql语句怎么写

java - 带有json的Spring rest junit帖子

python - 使用多个并行线程分部分下载大文件

php - 如何通过 PHP/cURL 使用不同的服务器 IP?

php - 通过单击图像保存变量

json - go json 结构中的控件显示