Getting Data via a PHP Curl Call - php

I am trying to make a PHP Curl Call. The call works in POSTMAN and looks like :
URL Endpoint: https://api.sb.example.com/v1/resource/identifier
Headers:
Content Type: application/json
Authorization : Bearer AccessTokenValue
HTTP Protocol : GET
This call returns me a valid JSON response. Now when i do the same using PHP, i am unable to get the response. Here is my PHP Curl call:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sb.example.com/v1/resource/identifier");
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/json',
'Authorization : Bearer AccessTokenValue'
));
$data = curl_exec($ch);
print_r($data);
curl_close($ch);
This call via PHP gives me the error:
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Any ideas/suggestions for the code ?

Please Add below said lines before curl_exec()
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

So i figured out, i had a space in headers array in KeyValue pair.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/json', ..)
it should have been
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',

Related

cURL HTTP Request with PHP (Routific API)

I'm trying to make an HTTP Request in PHP using cURL. The Request goes to the Routific (Route Optimization) API -> https://docs.routific.com/v1.3.1/docs
The idea is to send a JSON with different waypoints and get an optimal route back. Unfortunately, I get the following error when executing the code:
{"error":"Missing network"}
My Code looks as follows:
<?php
$data_string = json_encode(file_get_contents('Routes.json'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.routific.com/v1/vrp");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: bearer API_KEY')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>
What is wrong?
Thanks!

Send Headers during a curl call

Hi I have to send a request to an api(pitney bowls geolocation) for which I need an access token from a site. In the website its mentioned as -
To get an access token, set header and request body and call the token URI:
Authorization: Basic {BASE64 ENCODED VALUE}
Content-Type: application/x-www-form-urlencoded
POST https://api.pitneybowes.com/oauth/token
grant_type=client_credentials
I am currently using the following code : -
$val= base64_encode ('{gR6Ov7fCmuzHcVXE6bsmcUOt3SXhmXlL}:{ud61in4FYSGyF0eU}');
$ch = curl_init();
$headr = array();
$headr[] = 'Authorization: Basic {'.$val.'}';
$headr[] = 'Content-Type: application/x-www-form-urlencoded';
$headr[]='grant_type=client_credentials';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_URL, "https://api.pitneybowes.com/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
However am not getting any result. Any idea what might be wrong?
The third header you are adding, is not a valid header.
You should probably add that as a POST key-value pair:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');

Bad HTTP "Content-Type" header from the Dropbox API

When I try a simple curl request to the Dropbox API (v2), I get this error:
Error in call to API function "users/get_current_account": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".
The PHP code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/users/get_current_account" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $accessToken) );
echo curl_exec($ch);
curl_close($ch);
Using the CURLOPT_POST option specifies a content type (to application/x-www-form-urlencoded, based on the documentation). It's possible instead to make it a POST request using the CURLOPT_CUSTOMREQUEST option.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/users/get_current_account" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $accessToken) );
echo curl_exec($ch);
curl_close($ch);

cURL cannot connect to host

I am getting the issue in curl function. When I run the url direct in browser then it shows the content but when I use that url in curl function then it is showing the following message.
Array
7
couldn't connect to host
bool(false)
I am using the following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$queryurl);
curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Host: http://cloud.softpanda.com.au'
));
The URL you specified returns a 301 redirect to http://www.softpanda.com.au, so you will likely not get the content unless you either correct the URL or set CURLOPT_FOLLOWLOCATION to TRUE, which will follow redirects such as what you're getting.

how to read a response back from URL which is sending the response in json

I have been given an API url feed which returns response in JSON format and I have been told to set the headers to this:
Accept: application/json
X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009
Could anyone please tell me how to move ahead with this problem?
Question 1
I would use the PHP curl libraries.
For example:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009',
));
// grab URL and pass it to the browser
echo curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
See curl_setopt() for more information on the constants such as CURLOPT_HTTPHEADER I have used above.
Question 2 from comments
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009',
));
// grab URL and pass it to the browser
$json = json_decode(curl_exec($ch), true);
// close cURL resource, and free up system resources
curl_close($ch);
$json now contains an associative array of the response, which you can var_dump()to see the structure.
If you're using cURL in PHP you can do set custom headers on the request with:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009'
));

Categories