curl中的paypal MALFORMED REQUEST错误
Posted
技术标签:
【中文标题】curl中的paypal MALFORMED REQUEST错误【英文标题】:paypal MALFORMED REQUEST error in curl 【发布时间】:2016-09-11 21:32:24 【问题描述】:我在 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
为 authorization 调用 curljsonlint.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"
不知道我错在哪里?有人知道吗?提前致谢!
【问题讨论】:
谈论艰难的做事方式......使用json_encode()
从数组创建JSON字符串。不会出错
仅供参考 "expire_month": 07
无效或更具体地说,07
是 JSON 的无效数字
$result = curl_exec($ch)
在此之后添加 $ar = json_encode($result,true);
【参考方案1】:
您的问题是"expire_month": 07
中的07
。我假设这个值是一个字符串。不确定您粘贴到 jsonlint 的内容,但不是您问题中的 JSON。
如 cmets 所示,不要滚动您自己的 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));
【讨论】:
是的,您可能是对的,但在这种情况下,它显示 payer.funding_instruments[0].credit_card.expire_year","issue":"Must be numeric (YYYY) @user2134 :我认为这是因为您一年只传递 2 位数字,但需要 4 位数字 developer.paypal.com/docs/rest/api/vault【参考方案2】:如错误所示,您的 JSON 格式不正确。您无需通过 \
来转义您的请求参数。尝试删除这些或尝试这个开箱即用的示例代码。
<?php
$data = 'response_type=token&grant_type=client_credentials';
$headers_arr = array();
$headers_arr[]="Accept-Encoding:application/json;charset=utf-8";
$headers_arr[]="Accept-Language:en_US";
// $headers_arr[]="Authorization:Bearer AZHFuBBAIG1rP98P7Svn2WOVatM5KAllu6KvrTE8GGT4gt9vdfj8TtR_1Cer:EBTMERCurtbf_4auykCeGWYGvwsy147bSb3xCuYpohmouVBGTaYFUIRwWgbx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// this is used to bypass the SSL protocol, not recommended.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// this is used to bypass the SSL protocol, not recommended.
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "ARLg-BBMeTGLU5IHCsdIKEC_R_sMAkWM-yLsI5W5u9RuhvNhgolhYv-1cWmr:EJdAYxDx29McgMKOWjGK1OLHyaIxQBRuV9sWgfFSeMKGVBe-1jdiNgv5TLtP");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_arr);
$result = curl_exec($ch);
$res = json_decode($result);
$access_token = $res->access_token;
//now create a paypal payment
$headers_arr = array();
$headers_arr[]="Content-Type:application/json;charset=utf-8";
$headers_arr[] = "Authorization: Bearer $access_token";
// echo '<pre>';
// print_r($headers_arr);
$data_string = '
"intent":"authorize",
"payer":
"payment_method":"credit_card",
"funding_instruments":[
"credit_card":
"number":"4446283285273500",
"type":"visa",
"expire_month":11,
"expire_year":2018,
"cvv2":"874",
"first_name":"Betsy",
"last_name":"Buyer",
"billing_address":
"line1":"111 First Street",
"city":"Saratoga",
"state":"CA",
"postal_code":"95070",
"country_code":"US"
]
,
"transactions":[
"amount":
"total":"7.47",
"currency":"USD",
"details":
"subtotal":"7.41",
"tax":"0.03",
"shipping":"0.03"
,
"description":"This is the payment transaction description."
]
';
// echo data_string;
$chs = curl_init();
curl_setopt($chs, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/payments/payment');
curl_setopt($chs, CURLOPT_POST, true);
curl_setopt($chs, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($chs, CURLOPT_TIMEOUT, 45);
curl_setopt($chs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chs, CURLOPT_POST, 1);
curl_setopt($chs, CURLOPT_HTTPHEADER, $headers_arr);
if(curl_exec($chs) === false)
echo 'Curl error: ' . curl_error($chs);
$results = curl_exec($chs);
$res = json_decode($results);
echo 'ress=<pre>';
print_r($results);
【讨论】:
以上是关于curl中的paypal MALFORMED REQUEST错误的主要内容,如果未能解决你的问题,请参考以下文章
PayPal:MALFORMED_REQUEST,没有其他活动
PayPal 请求不断收到 MALFORMED_REQUEST