3

I'm trying to send headers using php curl - which should be rather simple - but there seems to be an issue.

Running on PHP 7.2 I'm setting the headers with

curl_setopt($ch, CURLOPT_HTTPHEADER, array('My-custom-header-name' => 'Header-Value'));

When trying to print info before curl_exec with

curl_getinfo($ch);

I have the following result :

enter image description here

The header part remains empty, is it because it shows response headers ? If yes, how to ensure that the headers are set correctly ?

I have an access to the remote address I'm trying to reach and I can see, well, can't see, the previously set headers. I would like to make sure they are attached to the curl request before investigating somewhere else.

The same request is working fine from local to remote addr, are there changes between php 7.1 and 7.2 that I'm not aware of ?

EDIT : I added

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

but now the following :

curl_getinfo($ch, CURLINFO_HEADER_OUT);

gives

POST /someurl HTTP/1.1
Host : Some host
Accept: */*
Content-Length: 153
Content-Type: application/x-www-form-urlencoded

I don't see my custom headers.

Thank you very much for your time.

thiout_p
  • 717
  • 11
  • 15

3 Answers3

11

Your array should be actually an array of lines! not array of different objects.

So, this should make it.

array(
    'My-custom-header-name: Header-Value'
    )

Like this:

curl_setopt($ch, CURLOPT_HTTPHEADER,     array(
        'My-custom-header-name: Header-Value'
        ));
Marco
  • 1,172
  • 9
  • 24
  • With this format I can see my headers! Thanks! Now I can focus on why the server gives me a 401 whereas it accept requests from local computer. Thank you very much :) – thiout_p Jan 15 '19 at 19:11
0

you need to use option CURLINFO_HEADER_OUT for the request headers.

This is only set if the CURLINFO_HEADER_OUT is set by a previous call to curl_setopt().

see the documentation for all available option flags.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I added the CURLINFO_HEADER_OUT in both curl_setopt and curl_getinfo($ch, CURLINFO_HEADER_OUT), but this time, the curl_getinfo returns nothing ... – thiout_p Jan 15 '19 at 18:49
  • @thiout_p the documentation does not tell to add it to both. it needs to be set before the request is being sent out; `curl_getinfo()` then has the value after the request was sent. – Martin Zeitler Jan 15 '19 at 18:52
  • I was printing curl_getinfo after closing the curl, my bad, I'm updating my post. – thiout_p Jan 15 '19 at 19:01
0

I struggled with this for about 15 minutes before I found out: the syntax of my headers was wrong!

Header-Name: Header-Value

is the right syntax. If you're including it on the command line, like so,

curl -H "Header-Name: Header-Value"

be sure to escape characters where needed (Windows and Linux are very different here). If you've got your headers in a file, it's much easier:

curl -H @headerFile.txt

Verify your request by including -v (lower case) for verbose output:

curl -v
Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23