0

I want to print out the contents of HTTP response headers. Here is the code:

$result = file_get_contents('http://www.google.com/');

print_r(json_decode($result, true));

The allow_url_fopen is 'On' in php.ini. I have tried the same with the curl extension of Php. The code was right but it printed nothing.Can anyone explain me why is it so?

ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
  • That's because the contents from `http://www.google.com` isn't a valid json to be decoded. Use `print_r($result);` instead and see if it works. – Felippe Duarte Jan 19 '17 at 15:05
  • No it doesn't solve my problem. It takes me to the google webpage. I want the contents of HTTP request such as status code, status text, header information etc. –  Jan 19 '17 at 15:08
  • What I'm showing to you is that everything is ok with your request, your server can comunicate with google and get a valid response. So, obviously, your problem is with your code which doesn't do what you want to do. What output do you expected? – Felippe Duarte Jan 19 '17 at 15:12
  • An array which contains the contents of the request made. –  Jan 19 '17 at 15:13
  • It seems to me that you are not exactly sure about what you want. Could you update your question with your expected output? – Felippe Duarte Jan 19 '17 at 15:15
  • output of type `/* output: stdClass Object ( [status_code] => 200 [status_txt] => OK [data] => stdClass Object ( [long_url] => http://sitepoint.com/ [new_hash] => 0 ) ) */` –  Jan 19 '17 at 15:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133567/discussion-between-jot-waraich-and-felippe-duarte). –  Jan 19 '17 at 15:18
  • this is not a request header but a response header – ᴄʀᴏᴢᴇᴛ Jan 19 '17 at 15:18
  • use curl to do this. See http://stackoverflow.com/questions/9183178/php-curl-retrieving-response-headers-and-body-in-a-single-request – ᴄʀᴏᴢᴇᴛ Jan 19 '17 at 15:19
  • Sorry i said request. surely it is a response header. I did the same with curl but it was also not showing anything. –  Jan 19 '17 at 15:20

1 Answers1

-1

file_get_contents is for literally getting the contents of the file. Based on the info provided it sounds like you want to call http://www.google.com and obtain the http status code. You need to use something curl, not file_get_contents. There are tons of tutorials and info on curl like this one

E LaRoche
  • 1,106
  • 1
  • 7
  • 8