PHP get_headers() 替代方案

发布于 2024-11-09 03:36:40 字数 234 浏览 5 评论 0原文

我需要一个 PHP 脚本来读取每个 URL 请求的 HTTP 响应代码。

类似的

$headers = get_headers($theURL);
return substr($headers[0], 9, 3);

问题是 get_headers() 函数作为一项策略在服务器级别被禁用。所以它不起作用。

问题是如何获取 URL 的 HTTP 响应代码?

I need a PHP script that reads the HTTP response code for each URL request.

something like

$headers = get_headers($theURL);
return substr($headers[0], 9, 3);

The problem is the get_headers() function is disabled at server level, as a policy.So it doesn't work.

The question is how to get the HTTP response code for a URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赢得她心 2024-11-16 03:36:40

如果启用了 cURL,您可以使用它来获取整个标头或仅获取响应代码。以下代码将响应代码分配给 $response_code 变量:

$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 请求,如下所示:

$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 );

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 );

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 );
一身软味 2024-11-16 03:36:40

如果可以,请使用 HttpRequest: https://www.php.net/manual/ en/class.httprequest.php

$request = new HttpRequest("http://www.example.com/");
$request->send();
echo $request->getResponseCode();

或者用困难的方法:https://www.php.net/manual/en/function.fsockopen.php

$errno = 0;
$errstr = "";

$res = fsockopen('www.example.com', 80, $errno, $errstr);

$request = "GET / HTTP/1.1\r\n";
$request .= "Host: www.example.com\r\n";
$request .= "Connection: Close\r\n\r\n";

fwrite($res, $request);

$head = "";

while(!feof($res)) {
    $head .= fgets($res);
}

$firstLine = reset(explode("\n", $head));
$matches = array();
preg_match("/[0-9]{3}/", $firstLine, $matches);
var_dump($matches[0]);

Curl 可能也是一个不错的选择,但最好的选择是击败你的管理员;)

Use HttpRequest if you can: https://www.php.net/manual/en/class.httprequest.php

$request = new HttpRequest("http://www.example.com/");
$request->send();
echo $request->getResponseCode();

Or do it the hard way: https://www.php.net/manual/en/function.fsockopen.php

$errno = 0;
$errstr = "";

$res = fsockopen('www.example.com', 80, $errno, $errstr);

$request = "GET / HTTP/1.1\r\n";
$request .= "Host: www.example.com\r\n";
$request .= "Connection: Close\r\n\r\n";

fwrite($res, $request);

$head = "";

while(!feof($res)) {
    $head .= fgets($res);
}

$firstLine = reset(explode("\n", $head));
$matches = array();
preg_match("/[0-9]{3}/", $firstLine, $matches);
var_dump($matches[0]);

Curl may be also a good option, but the best option is to beat your admin ;)

久光 2024-11-16 03:36:40

您可以使用 fsockopen 和常规文件操作构建和读取自己的 HTTP 查询。查看我之前对此主题的回答:

除了 CURL 之外,Rest 客户端还有其他选择吗?

You can build and read your own HTTP queries with fsockopen and regular file operations. Check out my earlier answer on this topic:

Are there any other options for rest clients besides CURL?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文