因为我需要在我的项目中使用paypal支付。这是我从官方paypal页面得到的。https://developer.paypal.com/docs/api/payments.payouts-batch/
curl -v -X POST https://api.sandbox.paypal.com/v1/payments/payouts \
-H "Content-Type:application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
"sender_batch_header": {
"sender_batch_id": "2014021801",
"email_subject": "You have a payout!"
},
"items": [
{
"recipient_type": "EMAIL",
"amount": {
"value": "9.87",
"currency": "USD"
},
"note": "Thanks for your patronage!",
"sender_item_id": "201403140001",
"receiver": "anybody01@gmail.com"
},
{
"recipient_type": "PHONE",
"amount": {
"value": "112.34",
"currency": "EUR"
},
"note": "Thanks for your support!",
"sender_item_id": "201403140002",
"receiver": "91-734-234-1234"
},
{
"recipient_type": "PHONE",
"amount": {
"value": "5.32",
"currency": "USD"
},
"note": "Thanks for your patronage!",
"sender_item_id": "201403140003",
"receiver": "408-234-1234"
},
{
"recipient_type": "PHONE",
"amount": {
"value": "5.32",
"currency": "USD"
},
"note": "Thanks for your patronage!",
"sender_item_id": "201403140004",
"receiver": "408-234-1234"
}
]
}'
从此我能够在php中做到这一点。
$ch = curl_init();
$header_object = (object) array('sender_batch_header'=>(object) array('sender_batch_id'=>"2014021801",'email_subject'=>"you got that"));
$data_array = array('items'=>array(
"recipient_type"=>"EMAIL",'amount'=>array('value'=>'9.87','CAD'),'note'=>'get your money',"sender_item_id"=>"232211",'receiver'=>"prabhs36@gmail.com"));
$data = array($header_object,$data_array);
// print_r($object);
// die();
$c_header = ['Content-Type'=>'application/json"',
'Authorization'=>'Bearer access_token_string_was_here'];
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payouts");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$c_header);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTQUOTE, $data);
// grab URL and pass it to the browser
$data_2 = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
我实际上无法理解什么是-d这意味着我能理解,或者我认为是$data
变量是我所做的。但我得到的错误是类std类的对象无法转换为字符串。和数组到字符串转换。
任何人都可以告诉我错误以及应该做些什么。
答案 0 :(得分:1)
-d
表示data
,请参阅https://curl.haxx.se/docs/manpage.html#-d
您应该使用json_encode
将数据编码为JSON并将其放入CURLOPT_POSTFIELDS
选项
$data = ['name' => 'Alice', 'age' => '25'];
$dataStr = json_encode($data);
$ch = curl_init('http://example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($dataStr)
]);
$result = curl_exec($ch);
<强>更新强>
要对数据进行编码,只需将其放入数组:
$data = [
'sender_batch_header' => [
'sender_batch_id' => '2014021801',
'email_subject' => 'You have a payout!',
],
'items' => [
[
'recipient_type' => 'email',
// ...
],
// ...
],
];
<强> UPDATE2 强>
考虑使用像Guzzle这样的HTTP客户端。它为发出HTTP请求提供了简洁的界面。案例:
$client = new GuzzleHttp\Client();
$data = [
'sender_batch_header' => [
'sender_batch_id' => '2014021801',
'email_subject' => 'You have a payout!',
],
'items' => [
[
'recipient_type' => 'email',
// ...
],
// ...
],
];
$res = $client->request('POST', 'https://api.sandbox.paypal.com/v1/payments/payouts', [
'headers' => [
'Authorization' => 'Bearer Access-Token',
],
'json' => $data,
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"batch_header":{"sender_batch_header"...