Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中忽略包含文件内容的移动头?_Php - Fatal编程技术网

如何在PHP中忽略包含文件内容的移动头?

如何在PHP中忽略包含文件内容的移动头?,php,Php,我已经为一个简单的内容用户编写了程序,它使用file_get_内容,但不幸的是,对于我的IP,该站点现在给出了一个302错误,该错误会转发到图像。对于所有其他用户,可以查看正常站点 如何重写get_内容,使其只下载网站内容,而不实际遵循重定向 $html = file_get_contents("http://www.site.net/"); 那里没有内容。在发送任何内容之前,HTTP响应中会发生重定向 服务器决定要看什么(或不看什么)。您需要创建一个上下文: $context = strea

我已经为一个简单的内容用户编写了程序,它使用file_get_内容,但不幸的是,对于我的IP,该站点现在给出了一个302错误,该错误会转发到图像。对于所有其他用户,可以查看正常站点

如何重写get_内容,使其只下载网站内容,而不实际遵循重定向

$html = file_get_contents("http://www.site.net/");

那里没有内容。在发送任何内容之前,HTTP响应中会发生重定向


服务器决定要看什么(或不看什么)。

您需要创建一个上下文:

$context = stream_context_create(
    array (
        'http' => array (
            'follow_location' => false // don't follow redirects
        )
    )
);
$html = file_get_contents('http://www.site.net/', false, $context);
请参阅手册:


话虽如此,页面上很可能没有内容了。提供
302
头和HTTP正文并非不可能,但这显然是非正统的。

我在通过直接链接访问Google Drive内容时遇到了这样的问题

错误方式:调用文件后\u获取\u内容返回302临时移动

//Any google url. Thsi example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$html = file_get_contents($url);
echo $html; //print none because error 302.
好方法:使用下面的代码,它再次工作:

//Any google url. Thsi example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";

$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);

echo $html;

我今天测试了它,2018年3月19日

重定向发生在HTTP响应头上。但是,配置错误的服务器可能会发送输出,即使它正在发送一个新位置以及响应代码302。此答案不正确(为避免任何疑问)。正如@Oliver所说,假设302不发送任何内容是不安全的。请记住,“follow_location”仅在PHP 5.3.4中可用(每个人都应该运行比它更新的版本)。此外,内部数组中的“max_redirects=>0”也是必需的(请参见上的示例2)不是
file\u get\u contents
。你知道你写了什么吗?例如,文件内容是错误的。