Reputation: 394
As I need to use paypal payout in my project. This is what I got from the official paypal page.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": "[email protected]"
},
{
"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"
}
]
}'
and from this I was able to make this in 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'=>"[email protected]"));
$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);
I actually can't understand what is -d here means so what i could understand ,or i think it is , the $data
variable is what i made. but I am getting the error that object of class std class cannot be converted to string. and array to string conversion.
Can anybody actually tell me that what is wrong and what should be done instead.
Upvotes: 0
Views: 289
Reputation: 385
-d
means data
, see https://curl.haxx.se/docs/manpage.html#-d
You should encode data to JSON using json_encode
and put it into CURLOPT_POSTFIELDS
option
$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);
UPDATE
To have your data encoded, just put it into array like that:
$data = [
'sender_batch_header' => [
'sender_batch_id' => '2014021801',
'email_subject' => 'You have a payout!',
],
'items' => [
[
'recipient_type' => 'email',
// ...
],
// ...
],
];
UPDATE2
Consider using some HTTP client like Guzzle. It provides clean and simple interface for making HTTP requests. Example for your case:
$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"...
Upvotes: 1