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( "
", curl_exec( $curl ) );
curl_close( $curl );
这篇关于PHP get_headers() 替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP get_headers() 替代
基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
