I need to perform an Http Request with the CURL method.
Here is the documentation of the service I have to connect with.
In order to invoke an API method, the client makes an HTTPS POST request on
https://app.kigo.net/api/ra/v1/method.
The request Content-Type header must be set to application/json and the post data (HTTP request body) must hold a JSON representation of input data.
Example of a valid HTTP request:
POST /api/ra/v1/ping HTTP/1.0
Host: app.kigo.net
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json
{
"PING" : "PONG"
}
The question is:
How to do it with CURL?
You need the SSL options for the https connection
followlocation will follow any redirects
return_transfer will save the response in a variable
$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 );
Related
i have curl command provided by some developer, i need to know how to call it in php.
Command is :
curl -H "x-api-key:Â xxxxxxxxxxxxxxxxxxxxxx"
"https://api.civicengine.com/office-
holders?address=1060+W+Addison+Chicago+IL"
i have written this
$data['api-key']='xxxxxxxxxxxxxxxxxxxxx';
/*$data['host']='https://api.civicengine.com';*/
$url='https://api.civicengine.com/office-holders?address=1060+W+Addison+Chicago+IL';
$ch = curl_init();
// set basic auth
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// set header accept
$headers = array('Accept:application/json', 'Accept-Language:en_US',"Authorization: Bearear xxxxxxxxxxxxxxxxxxxx");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// set mode to POST
curl_setopt($ch, CURLOPT_POST, 1);
// add post fields (1)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
print_r($result = json_decode(curl_exec($ch)));die;
i expect result but its giving
stdClass Object ( [message] => Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=FLUdjGvxuJ9liutivB0Ll5LW2t2xmv31lvK2guQi )
you forgot to add the x-api-key header to your php curl_ code. also your php code is altering the Accept Accept-Language, and Authorization header, your curl invocation proves that's not needed, and they may be incorrect to boot (idk), get rid of them while debugging (and once everything is working, you may add them back and check that they don't break anything..)
Follow this method to send api_key in header of API REQUEST
// Collection object
$ch = curl_init($url);
$headers = array(
"APIKEY: PUT_HERE_API_KEY",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml"
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
$result = curl_exec($ch); // execute
$result;
//show response
curl_close($ch);
The Response should be XML, but i'm getting error like (56): Failure when receiving data from the peer while sending the request to REST API using headers, Here is the sample request according to the client
POST http://api.toyotautrust.in/1.0/olx/inventory HTTP/1.1
User-Agent: Fiddler
Authorization: Token ******-****-****-****-***********
Host: api.toyotautrust.in
Content-Length: 52
Here is my Request code written in PHP using cURL
$headers1=[
'POST /1.0/olx/inventory HTTP/1.1',
'Host: api.toyotautrust.in',
'User-Agent: Fiddler',
'Authorization: Token' .$atoken1,
'Content-Length: 52'];
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'http://api.toyotautrust.in/1.0/olx/inventory');
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_HEADER, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_HTTPHEADER,$headers1);
$response1 = curl_exec($ch1);
print_r($response1);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $status_code1;
I have failed to send these two header in POST request
'User-Agent:Fiddler',
"Content-length: 0",
After adding these two headers, it's working fine. Before sending your request to server, Please ask server side people about headers and parameters
I'm working on a web_app using teamviewer API. According to the documentation, it works using HTTP requests (GET, POST, etc..).
I don't get how to make a request on PHP using CURL. The documentation said to do this:
POST /api/v1/oauth2/token HTTP/1.1
Host: webapi.teamviewer.com
Content-Type: application/x-www-form-urlencoded
so I wrote this
$url = 'webapi.teamviewer.com';
$headers = array(
'GET /api/v1/oauth2/authorize HTTP/1.1',
'Content-type: application/x-www-form-urlencoded'
);
//data
$data = [
'response_type' => 'authorization_code',
'redirect_uri' => 'https%3A%2F%2FTesting%2Ecom%2F',
'client_id' => '65671-XDdsxUNyxGmskcJHQgLC'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
I just can't figure out how to indicate the request the HTTP/1.1 and the path /api/v1/oauth2/authorize. I'm putting it in the header but I'm sure it doesn't go there.
I use to work on embedded C and I remember that first you connect to the host and then write GET path HTTP/1.1 as a command.
Your $url should be the full path, just like a normal URL. So it would be
$url = 'webapi.teamviewer.com/api/v1/oauth2/authorize';
And you can remove the GET ... part from $headers, because the URL (path) doesn't go in there.
That should work after that. If you want to see more info about the curl connection and what's going on, add this option:
curl_setopt($ch, CURLOPT_VERBOSE, true);
The full list of curl options is at the PHP manual page on curl_setopt.
I am working to connect with an API using curl and PHP
I have this CURL statement
curl -i --user api:YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1 --data-binary #test.png https://api.blahblah.com/blah
where YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1 is the api key and #test.png is the image file I need to transfer to the API for upload. What I don't get is how to write the curl for the link above
and I also have this Example Statement as given by the site for developers(looks a JSON)
POST /blah HTTP/1.1
Host: api.blahblah.com
Authorization: Basic YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1
Till now, I have written this up,
$url = 'https://api.tinypng.com/shrink';
$image = 'bf4lwp2.png';
$key = 'YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1';
$jsonscript = array(
'Host' => 'api.tinypng.com',
'Authorization' => $key );
$json_string = json_encode($jsonscript);
$ch = curl_init($url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'api:'.$key);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $image);
// Execute
$result=curl_exec($ch);
Also, if my request is a success, I get this...
HTTP/1.1 201 Created
Location: https://api.blahblah.com/blahblah.png
Content-Type: application/json; charset=utf-8
{
"input": {
"size": 87654
},
"output": {
"size": 102020,
"ratio": 0.236
}
}
I know I have to use file_get_contents() to get all that, but I don't know what URL to put in that function.
To get data from json first you can use json decode and than use foreach loop to
parse those data into variables
I'm trying to create a script that posts to an URL, using a list of proxies. The script seems to work fine without setting a proxy (CURLOPT_PROXY to null), but with a proxy set it does not seem to work. Does anybody know what I'm doing wrong here? The error I'm getting when posting with a proxy is:
ERROR
The requested URL could not be retrieved
Invalid Request error was encountered while trying to process the request:
POST /###/ HTTP/1.1
Host: ###
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 368
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------9b24e849cac0
Some possible problems are:
Missing or unknown request method.
Missing URL.
Missing HTTP Identifier (HTTP/1.0).
Request is too large.
Content-Length missing for POST or PUT requests.
Illegal character in hostname; underscores are not allowed.
And my code is:
$url = 'urltopost';
$proxy = 'proxy:port';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'postname1' => 'value1',
'postname2' => 'value2'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
You are missing the PROXYPORT option... You can't use the proxy:port notation
curl_setopt($ch, CURLOPT_PROXY, "YOUR PROXY HOST");
curl_setopt($ch, CURLOPT_PROXYPORT, "YOUR PROXY PORT");