PHP 如何获取file_get_contents时的HTTP status

阅读(1755)

通过$http_response_header这个超级全局变量可以获取到本次请求的HTTP status的值,当然实际中要配合try catch来用更合理。

file_get_contents("http://kaifage.com");
var_dump($http_response_header);

返回内容形如:


Array
(
    [0] => HTTP/1.1 500 Internal Server Error
    [1] => Date: Fri, 08 Apr 2016 02:20:23 GMT
    [3] => Content-Type: text/html; charset=utf-8
    [6] => Cache-Control: max-age=600
    [9] => Connection: close
)

这里列举了所有PHP保留的全局变量: http://php.net/manual/en/reserved.variables.php

via:
http://stackoverflow.com/questions/15620124/http-requests-with-file-get-contents-getting-the-response-code
http://php.net/manual/en/reserved.variables.httpresponseheader.php

Tags: