【问题标题】:How to make a curl request from this data如何从此数据发出 curl 请求
【发布时间】:2017-06-07 05:52:54
【问题描述】:

因为我需要在我的项目中使用贝宝付款。这是我从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 类的对象无法转换为字符串的错误。和数组到字符串的转换。 谁能真正告诉我哪里出了问题,应该怎么做。

【问题讨论】:

  • std 类是您得到的响应。当您收到错误时,问题将是处理响应

标签: php curl paypal


【解决方案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',
            // ...
        ],
        // ...
    ],
];

更新2

考虑使用一些 HTTP 客户端,例如 Guzzle。它为发出 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"...

【讨论】:

  • 所以我只需要对数据进行 json 编码。但你能确认我的 $data 是否与它提到的完全一样
  • $data = (object)[ 'sender_batch_header' => [ 'sender_batch_id' => '2014021801', 'email_subject' => '你有一笔款项!', ], 'items' => [ (object) [ 'recipient_type' => 'email', // ... ], // ... ], ];
  • 这是我在上面看到的格式。但仍然缺少一些东西。没有回应
  • 可能是CURLOPT_RETURNTRANSFER 选项(在您的代码中看不到)
  • 是的,我删除了它,但我仍然变得空白。你能去那个网址看看那里实际需要做什么吗
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2018-08-13
  • 2013-01-05
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
相关资源
最近更新 更多