How to Call Curl_Setopt with Curlopt_Httpheader Multiple Times to Set Multiple Headers

Can I call curl_setopt with CURLOPT_HTTPHEADER multiple times to set multiple headers?

Following what curl does internally for the request (via the method outlined in this answer to "Php - Debugging Curl") answers the question: No.

No, it is not possible to use curl_setopt(PHP) with CURLOPT_HTTPHEADER more than once, passing it a single header each time, in order to set multiple headers.

A second call will overwrite the headers of a previous call (e.g. of the first call).

Instead the function needs to be called once with all headers:

$headers = [
'Content-type: application/xml',
'Authorization: gfhjui',
];
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);

Related (but different) questions are:

  • How to send a header using a HTTP request through a curl call? (curl on the commandline)
  • How to get an option previously set with curl_setopt()? (curl PHP extension)

Setting CURL headers using CURLOPT_HTTPHEADER creates CORS problem

Setting some headers in the curl request dont changes header in your .php script.

UPDATE 1:

There is a parse error in your php script (As you found out by yourself):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization': 'someAuthorization',
'x-api-key': 'somekey',
'Content-Type': 'application/x-www-form-urlencoded'
));

Should be:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: someAuthorization',
'x-api-key: somekey',
'Content-Type: application/x-www-form-urlencoded'
));

The ajax call to this script result in an error 500 response which has default headers and NOT the headers you try to set in your .php script.

Enable error reporting in your local environment to see the problem "earlier".

for example in php.ini:

error_reporting=E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED 
display_errors=On

@see How do I get PHP errors to display?

cURL and redirects - returning multiple headers?

You can get the information of the total header size, and split the string up like this:

$buffer = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
$header_size = $curl_info["header_size"];
$header = substr($buffer, 0, $header_size);
$body = substr($buffer, $header_size)

Information taken from the helpful post by "grandpa".

Get the value of CURLOPT_HTTPHEADER from cURL

The headers shouldn't be an associative array, it should be an indexed array of strings.

$headers = [
'Try: Trying',
'Content-Type: text/html',
...
];

Then you should be able to access the header with: $_SERVER['HTTP_TRY'] since custom headers are prefixed with HTTP_

PHP cURL custom headers

curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12'
]);

https://www.php.net/manual/en/function.curl-setopt.php

Properly running a curl in php

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.

how to pass token parameter in GET request in php curl?

You're right that you have to add your header to CURLOPT_HTTPHEADER.

This can be done by adding to the PHP array object you're setting to CURLOPT_HTTPHEADER.

$headers = array(
"Content-Type: application/json; charset=utf-8",
"Authorization: Bearer ".$logged_user_token
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Here, you will be creating the php Array object $headers with both the Authorization and Content-Type headers. You then set your CURLOPT_HTTPHEADER to use that array of headers.



Related Topics



Leave a reply



Submit