gpt4 book ai didi

php - 如何从这些数据发出 curl 请求

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:09 25 4
gpt4 key购买 nike

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

最佳答案

-d 表示数据,参见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"...

关于php - 如何从这些数据发出 curl 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com