Posting Multidimensional Array through PHP cURL to PayPal - php

In the PayPal tutorial on making your first call it gives you a JSON string that I've found impossible to post. I've exhausted Google and I just can't seem to figure out how to post this data to PayPal. The code below gives me a 415 error code ("unsupported media type"). I've been stuck on the error for hours and am growing tired of it. I'll be grateful to anyone that can help me. I've tried to make my description clear in case we do find an answer others in the future will be able to find it easily.
$post_data = array (
'intent' => 'sale',
'redirect_urls' => array (
'return_url' => 'http://example.com/return',
'cancel_url' => 'http://example.com/cancel'
),
'payer' => array (
'payment_mehod' => 'paypal'
),
'transactions' => array (
'amount' => array (
'total' => '7.47',
'currency' => 'USD'
)
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $result_array['access_token']]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
$result = curl_exec($ch);
Edit: In the second to last line of provided code I have also used http_build_query and I've also tried just simply sending it as a multidimensional array. The error code 415 returns for both of these.

curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Authorization: Bearer ' . $result_array['access_token']
)
);

Related

OneSignal push notification sending to all user instead of one player

I am trying to send push notification to one device by its player_id stored in my database.
but whine it try it keep sending to all users
i don't know what is wrong in my code
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXX',
'include_player_ids ' => $player_ids,
'included_segments' => array(
'All'
),
'data' => $data,
'headings' => $title,
'contents' => $content,
'web_buttons' => []
);
$fields = json_encode($fields);
if($player_ids) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
just remove 'included_segments' => array('All'),
If you fill both of included_segments and include_player_ids, you will send to all selected player ids and also all selected segments. Segment All mean all subscribers.
you can read about other option on official documentation
I need to remove included_segments and web_buttons
both of them not just included_segments , when I try to just remove included_segments it give me error of {"errors":["You must include which players, segments, or tags you wish to send this notification to."]}
also I don't need 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'.

PHP curl POST for Watson Video Enrichment

Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:
//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey = 'xxxxxxxx';
//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => "simple.custom-model",
"upload" => array(
"url" => $url
)
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;
But I can't get it to work, even with varying CURLOPTs, like:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I keep getting the response: Bad Request.
Here's the API docs.
Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?
Reading their API documentation, it looks like the field preset is not of type string. Rather it has a schema defined here. This appears similar to how you are using the upload schema.
You should change your data array to look like this:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"video_url" => "https://example.com/path/to/your/video"
),
"upload" => array(
"url" => $url
)
);
Ok, I figured it out. Thanks to #TheGentleman for pointing the way.
My data array should look like:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"simple.custom-model" => array(
"video_url" => $url,
"language" => "en-US"
)
),
"upload" => array(
"url" => $url
)
);

Http 400 error when creating new Mailchimp list

I am trying to create a new list via the mailchimp api, I have tried tweaking the code so much but keep getting the error:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"0174d737-b3d4-40a6-9cd4-e934ed8578a7","errors":[{"
field":"","message":"Schema describes object, NULL found instead"}]}
My code (api key is valid):
$data = array( // the information for your new list--not all is required
"name" => $name,
"contact" => array (
"company" => $company,
"address1" => $address1
),
"permission_reminder" => $permission_reminder,
"use_archive_bar" => $archive_bars,
"campaign_defaults" => array(
"from_name" => $from_name,
"from_email" => $from_email,
"subject" => $subject,
"language" => $language
),
"notify_on_subscribe" => $notify_subs,
"notify_on_unsubscribe" => $notify_unsubs,
"email_type_option" => $type,
"visibility" => $visibility
);
$ch = curl_init("https://$dataCenter.api.mailchimp.com/3.0/lists/");
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($result); // display API response
I'm not seeing any JSON encoding of your payload in the code you provided. You will need to json_encode() your payload ($data) in order for MailChimp to be able to read it.
Like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Recurring payment in Adyen

I am trying to initiate a recurring payment in Adyen but I am unable to figure out how to do so. I have tried sending a request up receipt of payment results:
$request = array(
'amount.currency' => $this->currency,
'amount.value' => $sepaSubmission->amount,
'merchantAccount' => $this->merchantAccount,
'recurring.contract' => "RECURRING,ONECLICK",
'reference' => $sepaSubmission->psp_reference,
'shopperEmail' => $account->email,
'shopperReference' => $account->email,
"selectedRecurringDetailReference" => "LATEST",
"skinCode" => env('ADYEN_SKIN_CODE'),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, env('ADYEN_USERNAME') . ":" . env('ADYEN_PASSWORD'));
curl_setopt($ch, CURLOPT_URL, "https://test.adyen.com/hpp/pay.shtml");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST,count($request));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
I get the following error: Error: Skin null does not exist
I have verified a valid skinCode is included.
I am using SepaDirect as payment.
I have also just tried attaching the above fields to the initial payment submission form I am using and they are essentially ignored, the payment is processed as a one off.
Any assistance would be appreciated, I have been combing through the documents for several days to get this going to no avail.
It seems you are trying to redirect to the skin in order to do a followup Sepa transaction. This because you are making the call to "https://test.adyen.com/hpp/pay.shtml".
Sepa direct debit can be processed directly via the API, there is no need to send the shopper to the HPP
You can do the following:
$request = array(
'amount.currency' => $this->currency,
'amount.value' => $sepaSubmission->amount,
'merchantAccount' => $this->merchantAccount,
'recurring.contract' => "RECURRING",
'reference' => $sepaSubmission->psp_reference,
'shopperEmail' => $account->email,
'shopperReference' => $account->email,
"selectedRecurringDetailReference" => "LATEST",
"shopperInteraction" : "ContAuth",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, env('ADYEN_USERNAME') . ":" . env('ADYEN_PASSWORD'));
curl_setopt($ch, CURLOPT_URL, "https://pal-test.adyen.com/pal/servlet/Payment/v25/authorise");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST,count($request));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
Please note the change in URL, the removal of the skincode and the addition of "shopperInteraction" : "ContAuth", and the removal of one click in
recurring.contract' => "RECURRING",
So when you want to charge a shopper again, you just do this call from your end, there is no need to send him to the HPP.
Hope this helps,
Cheers,
Andrew

Why is this PHP script the JSON results when I haven't told it to?

So I'm using the only source I've found for sending a post request to Google QPX API. I want to save it in a json_decoded PHP array, but for some reason the $result = curl_exec($ch); line doesn't work, and the json prints onscreen anyways.
Is there something I'm not understanding that is happening in the cURL? Thanks!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
This:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
http://php.net/curl_setopt
Set this option to true if you want to save the result in a variable.

Categories