CURL modifying request headers - php

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.

Related

How to remove charset from php curl post?

Server that I am posting data to requires header that includes
Content-Type: application/json
I have a PHP code to set this header (edit)
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_SSLKEY, $mtlsKey); // --key
curl_setopt($ch, CURLOPT_SSLCERT, $mtlscert); // --cert
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token",
"X-Req-Signature: $XReqSig",
"Accept: application/json",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_URL,$base_url);
$data = curl_exec($ch);
I tried to change true values to false
tested also moving HTTPHEADER to beginning
When I check my request headers I get this
[content_type] => application/json;charset=utf-8
How do I remove charset from header? (or prevent php/server adding it?)

Properly running a curl in php

I am doing research for a university task where we need to run this cURL code in PHP. Is there any way that this can be done?
What's the right syntax?
curl -X GET "https://secure.fusebill.com/v1/customers/{id}/Overview" \
-H "Content-Type: application/json" \
-H "Authorization: Basic {APIKey}"
Here is one way to do it with curl and an options array:
<?php
$curl = curl_init("https://secure.fusebill.com/v1/customers/{id}/Overview");
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic {APIKey}",
"Content-Type: application/json"
)
));
$response = curl_exec($curl);
curl_close($curl);
You can alternatively set each option by calling curl_setopt($curl, OPTION_NAME, "value"); for each option in place of curl_setopt_array();:
$curl = curl_init("https://secure.fusebill.com/v1/customers/{id}/Overview");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Authorization: Basic {APIKey}",
"Content-Type: application/json"
));
$response = curl_exec($curl);
curl_close($curl);
For this request in PHP, you initialize curl and store in a variable so you can add options to the request before executing.
Here is a breakdown of the options from the snippet above:
CURLOPT_RETURNTRANSFER - return data from the request as a string instead of outputting directly (useful if you need to use the data in another function somewhere).
CURLOPT_CUSTOMREQUEST - HTTP request method for the request
CURLOPT_HTTPHEADER - set headers for the request.
Here is how the PHP maps to the cURL above:
-X specifies the HTTP method and the URL. In PHP we set CURLOPT_CUSTOMREQUEST and set the URL when we initialize the cURL handler, you can optionally use CURLOPT_URL to set the URL instead.
-H - stands for headers for the request. In PHP we set CURLOPT_HTTPHEADER. We set the headers as an array since there are multiple headers.
Remember to replace {id} in the URL and {APIKey} in the authorization header of your request.
I will give you some examples you can Just copy and paste in your PHP file to check.
First this is my favorite and most efficient way to get information from other webpages or even insert information if you need to.
GET RESULTS FROM WEBPAGE BY GET METHOD(It would work for the work you need):
$ch = CURL_INIT();
$url = 'https://google.com';
CURL_SETOPT($ch, CURLOPT_URL, $url );
//CURL_SETOPT($ch, CURLOPT_PROXY, $ip); //IN CASE YOU NEED TO USE PROXY
//CURL_SETOPT($ch, CURLOPT_PROXYPORT, $port);
CURL_SETOPT($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0');
CURL_SETOPT($ch, CURLOPT_POST, 0);//Get instead of post
CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER, True);
CURL_SETOPT($ch, CURLOPT_FOLLOWLOCATION, True);
CURL_SETOPT($ch, CURLOPT_ENCODING, 'gzip, deflate');//Try to curl https://amazon.com WITHOUT THIS LINE, it would give you some extra encryption.
CURL_SETOPT($ch, CURLOPT_CONNECTTIMEOUT,90);
CURL_SETOPT($ch, CURLOPT_TIMEOUT,90);
$result = CURL_EXEC($ch);
echo $result;
Next is if you need to insert data in the page using a post method.
You can use this to LOG IN in some websites or you use to insert and get data automatically
$data = array(
"email" => "example#gmail.com",
"pwd" => "12341234",
"some other field"=> '123123'
);
$ch = CURL_INIT();
$url = 'https://google.com';
CURL_SETOPT($ch, CURLOPT_URL, $url );
CURL_SETOPT($ch, CURLOPT_POST, true); //Post request
CURL_SETOPT($ch, CURLOPT_POSTFIELDS, $data);
CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER,True);
CURL_SETOPT($ch, CURLOPT_FOLLOWLOCATION,True);
CURL_SETOPT($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) ."/cookie.txt");//I am saving here the cookies in case I need to go to another page or do some action after login
CURL_SETOPT($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) ."/cookie.txt");
CURL_SETOPT($ch, CURLOPT_FOLLOWLOCATION, true); //ALLOW REDIRECTION
CURL_SETOPT($ch, CURLOPT_CONNECTTIMEOUT,90);
CURL_SETOPT($ch, CURLOPT_TIMEOUT,90);
$result = CURL_EXEC($ch);
Second it is the most easy one that has some limitations and because of that I don't like:
$url = 'https://www.google.com/';
echo file_get_contents($url);
If you have any questions, let me know.
I work with curl all the time!

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

PHP Curl - Knowing the Content-type

Below is an example without CURLOPT_HTTPHEADER, and it works fine.
<?php
$URL = "http://someurl.info";
$data = 'x_test_request=0&x_version=3.1&x_delim_data=true&x_relay_response=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 1800);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
?>
Does Curl auto-detect the type of data we Post, and auto-count the string length?
Or should we always specify Both?
Or does it largely depend on the destination's rules and expectations?
To be safe, I usually add the header info like this:
<?php
$contentlen = strlen(trim($data));
$headersARR = array(
"Content-type: application/x-www-form-urlencoded",
"Content-length: ".$contentlen,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersARR);
?>
I read http://us1.php.net/curl_setopt, but haven't noticed any details on defaulted content-type parameters.
If you do not specify anything about Content-type but use the CURLOPT_POSTFIELDS then the header is always Content-type: application/x-www-form-urlencoded as default. Also it sets the Content-length: automatically.
For this reason, when we post JSON content through the POST we need to override the content-type through HTTPHEADER option. It is similar to XML or any other specific type of content.

How to use PHP cURL to send images with the correct Content-Type?

Objective:
I want to send multiple images using multipart/form-data. Below is a snippet of my code.
Problem:
On one PC, the multipart attachment is sent by the correct MIME header Content-Type: image/jpeg, but on another PC, the MIME header is Content-Type: application/octet.
Question:
How can I force cURL to set the correct Content-Type header for the MIME content?
$ch = curl_init();
$params = array('name' => '#D:\globe.jpg');
$base_url = "https://example.com"."?".varEncode($test_data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL,$base_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
Use the CURLOPT_HTTPHEADER option:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));
So specify Content-Type headers specifically for file uploads, use:
$params = array('name'=>'#D:\globe.jpg;type=image/jpeg');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

Categories