0

I am using cURL for custom HTTP header request. But how can I get PHP to display the headers?

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('my_key: 12345'));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
    echo "<br><br>";


    if($_SERVER['my_key']=='12345'){
        echo 'Key is OK';
    } else {
        echo 'Key is wrong';
    }


    // also tried!
    print_r($_COOKIE);
    var_dump (curl_getinfo($ch));

    ?>
Namita
  • 75
  • 6
  • Maybe see http://stackoverflow.com/questions/866946/how-can-i-see-the-request-headers-made-by-curl-when-sending-a-request-to-the-ser although there were other possibilities I found as well, so if that doesn't work, follow linked questions to see if you can find the answer you seek. – Guy Schalnat Jul 15 '15 at 03:31
  • Did you try getallheaders function ? – lhoppe Jul 15 '15 at 04:01

1 Answers1

0

I guess easier way for you might be guzzle. You just can do something like this:

$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', ['auth' =>  ['user', 'pass']]);

echo $res->getStatusCode();              // "200"
echo $res->getHeader('content-type');    // 'application/json; charset=utf8'
echo $res->getBody();                    // {"type":"User"...' 

More info in docs.

In other way, you may check this solution.

If you realy want to get your request, but not response headers, you should try this code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/');
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_getinfo ($ch);
/*[
    "url"                     => "http://www.google.com/",
    "content_type"            => "text/html; charset=UTF-8",
    "http_code"               => 302,
    "header_size"             => 267,
        ...
    "redirect_url"            => "http://www.google.com.ua/?gfe_rd=cr&ei=-fClVYDfA9eEtAH65oGQAQ",
    "request_header"          => "GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept: * /*\r\n\r\n"
]*/
Community
  • 1
  • 1
Sergey Chizhik
  • 617
  • 11
  • 22