使用 PHP 获取 HTTP 响应标头/重定向状态
Hej,
我目前正在开发一个基于 PHP 的工具来监视相当大量的 URL 及其重定向状态。我花了相当多的时间来寻找获取 HTTP 响应标头内容以提取当前重定向代码和位置的最佳方法。目前的做法是这样的:
$resource = fopen( $url, 'r' );
$metadata = stream_get_meta_data( $resource );
$metadata = $metadata['wrapper_data'];
// Looping through the array to find the necessary fields
这适用于我正在监控的 95% 的 URL。对于更多的问题,我通过在执行重定向之前解析网站返回的实际 HTML 来解决这个问题,因为它包含类似“此网站已移至此处”的内容。这似乎不是一个非常强大的解决方案,但它在某些情况下有所帮助。
这仍然给我留下了许多无法自动检查的 URL。
像 Ask Apache HTTP Headers Tool 这样的工具似乎更可靠,我想知道获取重定向信息的更好方法是什么?
Hej there,
I am currently working on a PHP-based Tool to monitor a rather large number of URLs and their redirect status. I have spent quite some time on finding the best way to fetch the content of the HTTP response headers to extract the current redirect code and location. This is how it's done at the moment:
$resource = fopen( $url, 'r' );
$metadata = stream_get_meta_data( $resource );
$metadata = $metadata['wrapper_data'];
// Looping through the array to find the necessary fields
This works on 95% of the URLs I'm monitoring. For a few more I have solved it by parsing the actual HTML the website returns before the redirect is executed since it contained something like "This website has been moved here". This does not seem to be a very robust solution, but it helped in a few cases.
This still leaves me with a number of URLs I can not check automatically.
Tools like Ask Apache HTTP Headers Tool seem to be more reliable and I was wondering what could be a better way to obtain the redirect information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还可以尝试curl,它是检索所有标头的最短示例,如下所示:
输出:
当然,这只是您想要的标头,然后fsockopen 也可以工作。除了 GET 之外,您应该使用 HEAD,因为您只需要标头,而不是内容。
此外,curl 也适用于 https url-s(前提是您已使用 ssl 支持编译它)。
You could also try out curl, shortest possible example that retrieves all the headers looks like this:
Output:
Of course, it is just headers you want, then fsockopen works just as well. Except that instead of GET, you should use HEAD, because you just want the headers, not the content.
Also, curl works (provided you have compiled it with ssl support) for https url-s as well.