即使PHP返回404,也要下载PHP中的URL内容 [英] Download the contents of a URL in PHP even if it returns a 404

查看:72
本文介绍了即使PHP返回404,也要下载PHP中的URL内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使HTTP响应代码为404,我也想使用PHP下载URL的内容.file_get_contents会出错,并且我无法使用Google找到答案.我该怎么办?

I want to download the contents of a URL using PHP, even if the HTTP response code is 404. file_get_contents will error out, and I wasn't able to find an answer using Google. How can I do this?

推荐答案

默认情况下

By default file_get_contents only returns the content of HTTP 200 responses.

使用curl可以分别获取标题和内容.

从PHP 5.0开始,您还可以为file_get_contents指定上下文,从而无需依赖url就可以执行此操作(请参阅Gordon的答案).

As of PHP 5.0, you can also specify a context for file_get_contents, allowing you to do it without relying on url (See Gordon's answer).

这篇关于即使PHP返回404,也要下载PHP中的URL内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆