Reputation: 11386
Somehow I cannot set custom headers for the PHP curl call. The following code is working on my server (x86_64-redhat-linux-gnu, curl version 7.40.0) but it does not work on the server of our customer (x86_64-pc-linux-gnu, curl version 7.43.0).
$bodyString = json_encode($body);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$customerId:$licenceKey",
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($bodyString),
),
CURLOPT_POSTFIELDS => $bodyString
);
log("Requesting [".$url."]...");
$cURL = curl_init();
curl_setopt_array($cURL, $options);
$response = curl_exec($cURL);
log("Header dump:\n ".curl_getinfo($cURL, CURLINFO_HEADER_OUT));
On our server it works as expected and CURLINFO_HEADER_OUT returns:
POST /api/rest_call?p1=true&p2=true HTTP/1.1
Authorization: Basic *****
Host: admin.myserver.net
Accept: */*
Content-Type: application/json
Content-Length: 119
On the customers server the CURLINFO_HEADER_OUT returns an empty string. The requested server returns "415 Unsupported media type" because no "Content-Type" was transferred.
UPDATE: Followed the hint from Php - Debugging Curl I created the verbose log file. Here is a part of it:
POST /api/rest_call?p1=true&p2=true HTTP/1.1
Host: admin.yoochoose.net
Accept: */*
All the custom headers and(!) the authentication information seems to be ignored by cURL. Any ideas?
Upvotes: 1
Views: 1924
Reputation: 21
You cannot use name containing '_' in your header, try using '-' instead.
If you want you need to set underscores_in_headers on in your nginx config.
More details here
Upvotes: 2
Reputation: 11386
Solved. CURLOPT_FOLLOWLOCATION
must be removed. For POST requests it is useless anyway.
I cannot investigate it deeply. It is not my server. As far I as know it is linked to the PHP safe mode or similar restriction. It blocks local file system requests outside the www
folder and symlink usage. It affects the CURLOPT_FOLLOWLOCATION
options.
PHP safe_mode problem using curl
It is an old problem. But I did not know, it removes all the headers from the request as well.
Upvotes: 0