我编写了一个类来检测 cURL 是否可用,如果它使用 cURL 执行 GET、POST、DELETE。在 cURL 版本中,我使用 curl_getinfo($curl, CURLINFO_HTTP_CODE);
来获取 HTTP 代码。如果 cURL 不可用,它使用 fopen() 来读取文件内容。如何在没有 cURL 的情况下获取 HTTP header 代码?
最佳答案
使用get_headers :
<?php
$url = "http://www.example.com/";
$headers = get_headers($url);
$code = $headers[0];
?>
编辑:get_headers
需要额外调用,不适合这种情况。使用 $http_response_headers
作为 suggested by hakre .
关于PHP在没有cURL的情况下获取http头响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9724924/