1

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?

Community
  • 1
  • 1
30thh
  • 10,861
  • 6
  • 32
  • 42
  • 1
    Check here http://stackoverflow.com/questions/3757071/php-debugging-curl/14436877#14436877 how to debug your curl problem (verbos may be helpful..) – Svetoslav Nov 18 '15 at 13:13
  • Just remove 'Content-Length: ' . strlen($bodyString) and retry.. I am not sure but normally, Content-Length is a response header not request header. – akhilp2255 Nov 18 '15 at 13:34
  • Check MIME-type configuration on the client server for "application/javascript" – Max Zuber Nov 18 '15 at 13:57

2 Answers2

2

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

  • Thanks for this. Used underscores on another project where these were allowed, didn't think twice. So yes, using dashes, the server rewrites it to HTTP_ converting the hyphens to underscores. – Toby Crain Dec 13 '22 at 00:32
0

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.

Community
  • 1
  • 1
30thh
  • 10,861
  • 6
  • 32
  • 42