Send Headers during a curl call - php

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

Related

PHP cURL how to send a JSON POST request and also include a URL querystring?

I'm trying to submit a POST request with JSON data to an api endpoint. The endpoint requires a querystring passing the api credentials, but also requires the JSON data to be POSTed.
When I try to do this with PHP cURL as shown below, the querystring is apparently removed - thus the api is rejecting the request due to missing api key.
I can do this easily with Postman when testing access to the api endpoint.
How can I make the cURL request include both the querystring AND the JSON POST body?
Example code:
// $data is previously defined as an array of parameters and values.
$url = "https://api.endpoint.url?api_key=1234567890";
$ch = curl_init();
$json = json_encode($data);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You are doing almost right.
Sometimes you need to relax SSL verification.
Otherwise, update php ca bundle:
https://docs.bolt.cm/3.7/howto/curl-ca-certificates
Add the following:
$headers = array(
"Content-type: application/json;charset=UTF-8",
"Accept-Encoding: gzip,deflate",
"Content-length: ".strlen($json),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, "identity, deflate, gzip");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Sometimes you need to change encoding too:
$result = utf8_decode($result);
And check the returning data.

Curl xml Response in Php

i need to post Data in Xml to a server but whnen i use a basic curl post i have a redirect page to the curl url , but when i use something like postman it works just find , I even tried the code generated by postman and i got the same results (redirect)
here is my curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ["SAMLResponse"=> $xml]);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "content-type: application/x-www-form-urlencoded";
$headers[] = "authorization: Basic CODE";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = (curl_exec($ch));

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!

CURL modifying request headers

I'm hoping someone can help me. I'm sending an API request, via CURL, using PHP. The headers to the third party application need to be as follows:
$headers = array(
"content-type: application/json",
"Accept: application/json"
);
The CURL request is initialised and sent as follows:
// 1. initialize
$ch = curl_init();
// 2. set the options, including the url
curl_setopt($ch, CURLOPT_URL, $this->sendPropertyUrl);
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $this->certPath);
curl_setopt($ch, CURLOPT_CAINFO, $this->certPath);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->certPassword);
curl_setopt($ch, CURLOPT_POSTFIELDS, $propertyDataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 3. execute and fetch the resulting HTML output
$rightmoveResponse = curl_exec($ch);
$output = json_decode($rightmoveResponse, true);
However if I trap the header info actually sent in the CURL request the output is as follows:
POST /v1/property/sendpropertydetails HTTP/1.1
Host: adfapi.adftest.rightmove.com
Accept: */*
Content-Length: 1351
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Can anyone explain why CURL has modified the Accept and Content-Type parameters?
Any help greatly appreciated.
Brian
What you want is defined by CURLOPT_HTTPHEADER instead of CURLOPT_HEADER.
From PHP Docs:
CURLOPT_HEADER TRUE to include the header in the output.
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
See this user note about using it.
You used curl_setopt($ch, CURLOPT_POST, true); which sets the content-type. Instead use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); (see http://php.net/manual/en/function.curl-setopt.php).
This is in addition to what #Alan Machado said.

Getting Data via a PHP Curl Call

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',

Categories