Can I call curl_setopt with CURLOPT_HTTPHEADER multiple times to set multiple headers?
Solution 1:
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 the curl_setopt
call 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 = array(
'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)
Solution 2:
Other type of format :
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-length: 0';
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);