PHP Curl to form this Post Request - php

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.

Related

Understand api curl instruction

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 .

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);

curl to php to post a json and audio

I'm trying to convert a curl command to be used in a php script
curl -k -F "request={'timezone':'America/New_York','lang':'en'};type=application/json" -F "voiceData=#d8696c304d09eb1.wav;type=audio/wav" -H "Authorization: Bearer x" -H "ocp-apim-subscription-key:x" "http://example.com"
and here is my php script
<?
$data = array("timezone" => "America/New_York", "lang" => "en", "voiceData" => "d8696c304d09eb1.wav");
$data_string = json_encode($data);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer x',
'ocp-apim-subscription-key:x')
);
$result = curl_exec($ch);
?>
I understand that the send audio file bit is not right but i cant find an example how to post it.
I have edited this in response to the post fields but if I include $ch i get no output if don't include the output complains that no post request. Any ideas?
<?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer x',
'ocp-apim-subscription-key:x')
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
'voicedata' => new CURLFile("d8696c304d09eb1.wav")
)
);
$result = curl_exec($ch);
echo $result;
?>
You're missing the request= in your POST fields. Also, sending files using #filename is deprecated, you should use the CURLFile class.
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
'voicedata' => new CURLFile("d8696c304d09eb1.wav")
)
);

Ignore Content-Type: application/force-download in CURL

I need to read the size of a file but the server is forcing me to download it first.
I note one of the response headers is Content-Type: application/force-download and that seems to bypass my curl inputs...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
curl_exec($ch);
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
Any ideas?
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
These two lines reset CURLOPT_NOBODY.
CURLOPT_NOBODY changes method to HEAD and CURLOPT_POST changes it to POST.
Source: http://php.net/manual/en/function.curl-setopt.php
Curl conforms with the force-download header, but file_get_contents can be used to limit the file download download to only 1 byte. This solved the problem!
$postdata = http_build_query(
array(
'username' => "",
'password' => ""
)
);
$params = array(
'http' => array
(
'method' => 'POST',
'header'=>"Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
$ctx = stream_context_create($params);
file_get_contents($url,false,$ctx,0,1);
$size = str_replace("Content-Length: ","",$http_response_header[4]);

Categories