简体   繁体   中英

php curl not sending headers

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 :

在此处输入图片说明

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.

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'
        ));

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM