php curl not sending headers
Asked Answered
H

3

6

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.

Hoseahoseia answered 15/1, 2019 at 18:38 Comment(0)
H
15

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'
        ));
Harbert answered 15/1, 2019 at 19:7 Comment(1)
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 :)Hoseahoseia
S
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.

Stupid answered 15/1, 2019 at 18:41 Comment(3)
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 ...Hoseahoseia
@Hoseahoseia 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.Stupid
I was printing curl_getinfo after closing the curl, my bad, I'm updating my post.Hoseahoseia
P
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
Propel answered 29/4, 2021 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.