Monday 20 November 2017

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

itemprop="text">

Can I call href="http://php.net/curl_setopt">curl_setopt with
CURLOPT_HTTPHEADER multiple times to set multiple
headers?



$url =
'http://www.example.com/';


$curlHandle =
curl_init($url);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER,
array('Content-type: application/xml'));
curl_setopt($curlHandle,
CURLOPT_HTTPHEADER, array('Authorization: gfhjui'));

$execResult =
curl_exec($curlHandle);


Answer




Following what curl does internally for the
request (via the method outlined in href="https://stackoverflow.com/a/14436877/367456">this answer to "Php -
Debugging Curl"
) answers the question: No, it is not possible to
use the curl_setopt call with
CURLOPT_HTTPHEADER. The second call will overwrite the headers
of the first call.



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




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


Related
(but different) questions are:






No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...