How to receive headers from Curl?

Asked

Viewed 357 times

0

I’m making a request via Curl as follows (it is only test, not in production)

<?php

$post = [
    "Primeiro",
    "Segundo"
];

$headers = [
    'Terceiro',
    'Quarto'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, __DIR__ . '/er.php?teste');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

curl_close ($ch);

?>

And I’m trying to get dice AND THE HEADER as follows:

<?php

if ( isset($_GET["teste"]) ) {

    print_R ( $_SERVER["headers"] );
    print_R ( $_POST );

}
?>

The $_POST receive normally. But I’m not sure how to receive the headers.

I searched the exit print_R ( $_SERVER ); and the key headers there is no.

What should I do to receive the header sent by Curl?

EDIT:

I tried also as below and did not receive the header:

$headers = [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Accept-Language: en-US,en;q=0.5',
    'Cache-Control: no-cache',
    'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
    'Host: www.example.com',
    'Referer: http://www.example.com/index.php', //Your referrer address
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
    'X-MicrosoftAjax: Delta=true'
];
  • The array headers should be a array associative, where the key is the header name. The way it did it makes no sense. Fixing this, see if you will receive the value in $_SERVER["HTTP_<nome>"]

  • Okay. Can you explain to me how to do?

  • Do you know what HTTP headers are? You know how to create an associative array?

  • see at the end of the question please. I edited!

  • sorry, I do. But I wanted to reduce the amount of text in the question and I got in the way

  • I believe this can help you: https://stackoverflow.com/a/9183272/3687410 take a look.

  • Put the print_r($_SERVER) also

  • Yes, but how will print_r($_SERVER) help? if you don’t have neuma $_SERVER co Indice being index of the sent array? I was in doubt!

  • And with the name HTTP_<nome>, as I quoted in the first comment?

  • type like this? print_r ( $_SERVER["HTTP_X-Apple-Tz"] );

  • You saw the link I sent above?

  • I read yes and did some tests, but what I need does not come out in the variables!

Show 7 more comments

1 answer

1


To submit a request to Curl using headers you need to use:

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Terceiro: 42',
    'Quarto: 404'
]);

Meanwhile, the $headers must follow a pattern of: Nome: Valor.


At the other end, when you receive the request, you can use the $_SERVER in order to access them:

$_SERVER['HTTP_TERCEIRO']

The name should always start with HTTP_ and should always be capitalized, as well as replace any - for _.


This should return 42, which is the header value Terceiro.

  • rules...always them. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.