Understand api curl instruction - php

Stuck trying to get an api-connection to work. I believe I don't understand the below example request in the api. Especially the last row "grant_type..etc". How is this line to be handled in CURL? As POSTFIELDS? Get an error {"error":"unsupported_grant_type"}
POST /connect/token HTTP/1.1
Authorization: Basic base64(client_id:client_secret)
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
"grant_type=authorization_code&code=<authorization_code>&redirect_uri=<redirect_uri>"
Code so far:
$ch = curl_init('https://xxxx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded;charset=UTF-8',
'Authorization: Basic '.base64_encode($clientid.':'.$clientsecret).''
));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $_GET['code'],
'redirect_uri' => $redirecturi,
'grant_type' => 'authorization_code'
));
$result = curl_exec($ch);
curl_close($ch);
echo $result;

https://auth0.com/docs/applications/reference/grant-types-available
Here all grant type are explained .

Related

InfusionSoft: API error While Fetching contact Details

I'm working on InfusionSoft API (documentation here) and making request by using CURL (no API SDK) but getting the following error.
Failed to parse XML-RPC request: Premature end of file.
any help would be appreciated. Below is the CURL code that I'm using.
$url_token = 'http://api.infusionsoft.com/crm/xmlrpc/v1/?access_token='.$access_token;
$curlPost = array(
'access_token' => $access_token,
'privateKey' => $api_key,
'contactId' => 6171,
'selectedFields' => array('email')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));
$res = curl_exec($ch);
echo $res;

HTTP REQUEST in curl

how can I make an HTTP REQUEST in curl with json?
I need to put this in curl for php:
POST /api/ra/v1/ping HTTP/1.0
Host: app.kigo.net
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json
{
"PING" : "PONG"
}
Thanks in advance!
EDIT
This is what I've tried, but it shows "Invalid Content-Type header.":
<?php
$url = 'https://app.kigo.net/api/ra/v1/ping';
$headers = array( 'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'Content-Type' => 'application/json' );
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, "PING=PONG");
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );
?>
Your CURLOPT_HTTPHEADER array must be in the form array('Header1: value1', 'Header2: value2') and not array('Header1' => 'value1', 'Header2' => 'value2'), see examples in http://php.net/manual/en/function.curl-setopt.php
Also, if you post JSON data, you should probably json_encode() the data you are sending.
$headers = array(
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
'Content-Type: application/json'
);
$postData = json_encode(array(
'PING' => 'PONG'
));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://app.kigo.net/api/ra/v1/ping');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

Issues with PAYPAL using cURL

I'm trying to create a payment (Paypal)
My data provided by paypal : https://developer.paypal.com/webapps/developer/docs/api/
$data = '{All the data}';
cURL..
$ch = curl_init();
$headers = array(
'Content-type: application/JSON',
'Authorization: Bearer A101.YqvcjeJ6va_06Zd3ZQSkn8uNeH0Il1KJKCy72pc0ReSZ_n-cA9oEbVSArBc78F-e.iVCsmw63xoQHrfe7W1v_MYGHhUS',
);
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response= curl_exec($ch);
var_dump($response);
$json = json_decode($result);
print_r($json);
curl_close($ch);
The response:
[name] => UNKNOWN_ERROR
[message] => An unknown error has occurred
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#UNKNOWN_ERROR
[debug_id] => 58039c1674622
So guys, what is wrong with my code?
Thanks for the help!

SkyScanner API error when called by PHP

I did exactly as shown in the API documentation
http://business.skyscanner.net/portal/en-GB/Documentation/FlightsLivePricingList
but when i call it returns this error
HTTP/1.1 100 Continue HTTP/1.1 500 Internal Server Error Cache-Control: private Content-Type: application/json Date: Sat, 04 Jun 2016 06:23:48 GMT Connection: close Content-Length: 2 {}
and here is my code in PHP
<?
$url = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0/';
$data = array('apiKey' => 'de995438234178656329029769192274', 'country' => 'BR', 'currency' => 'BRL',
'locale' => 'pt-BR', 'originplace' => 'SDU-iata', 'destinationplace' => 'GRU-iata', 'outbounddate' => '2016-09-23',
$headers = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
printf($result);
?>
any idea what is going wrong?
thanks in advance for any kind
So I think the PHP is sending the wrong request type, because the HTTP headers were being sent as an array (so defaults to 'multipart/formdata'). If you use http_build_query on that array, it is sent correctly as 'x-www-form-urlencoded'.
I've tidied things up, removed some duplication in the curl options, and correctly get a 201 response on your example now:
<?
$url = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0/';
$data = array('apiKey' => 'de995438234178656329029769192274', 'country' => 'BR', 'currency' => 'BRL',
'locale' => 'pt-BR', 'originplace' => 'SDU', 'destinationplace' => 'GRU', 'outbounddate' => '2016-09-23', 'locationschema' => 'Iata', 'adults' => 1);
$httpdata = http_build_query($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $httpdata);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
Hope that helps, I'll keep an eye on the thread in case of anything else - feel free to drop us a query or check the FAQs here: https://support.business.skyscanner.net/hc/en-us

PHP Curl to form this Post Request

Can someone help me in forming this following POST request in PHP CURL ?
As mentioned in this URL : https://developer.amazon.com/public/apis/experience/cloud-drive/content/nodes
curl -v -X POST --form
'metadata={"name":"testVideo1","kind":"FILE"}' --form
'content=#sample_iTunes.mp4'
'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=testVideo1&suppress=deduplication'
--header "Authorization: Bearer
Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"
this is what am trying... i want to form the above mentioned type curl request to the amazon server so i can upload a file into it. as there is no examples available am struck in this... can someone helps on this ?
$fields = array(
'multipart' => array(
'name' => 'testupload',
'content' => array(
'kind' => 'FILE',
'name' => 'hi.jpg',
'parents' => $parents
)
)
);
$fields_string = json_encode($fields);
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $contenttype);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
You're almost there.
Add this on the curl code.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
This will able to access the url using SSL/HTTPS.

Categories