PHP get_headers()替代方案 [英] PHP get_headers() alternative
本文介绍了PHP get_headers()替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个PHP脚本来读取每个URL请求的HTTP响应代码。
I need a PHP script that reads the HTTP response code for each URL request.
类似
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
问题是在服务器级别禁用了get_headers()函数作为策略。所以它没有不行。
The problem is the get_headers() function is disabled at server level, as a policy.So it doesn't work.
问题是如何获取URL的HTTP响应代码?
The question is how to get the HTTP response code for a URL?
推荐答案
如果启用了cURL,您可以使用它来获取整个标题或只是响应代码。以下代码将响应代码分配给 $ response_code
变量:
If cURL is enabled, you can use it to get the whole header or just the response code. The following code assigns the response code to the $response_code
variable:
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://stackoverflow.com' ) );
curl_exec( $curl );
$response_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
curl_close( $curl );
要获得整个标头,您可以发出HEAD请求,如下所示:
To get the whole header you can issue a HEAD request, like this:
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://stackoverflow.com' ) );
$headers = explode( "\n", curl_exec( $curl ) );
curl_close( $curl );
这篇关于PHP get_headers()替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文