从PHP服务器端远程访问文件会得到301而不是文件,该怎么办?
编辑:答案在标记答案的注释中 我目前正在更新移动网站上的一些关键组件。该网站使用来自不同服务器的数据来显示学生时间表。最近,另一个网站(我对其没有任何控制权)受到了一次重大检修,我现在自然不得不更新移动网站 我试图做的是访问一个iCal文件并解析它。由于我正在处理的站点运行在一个既没有curl库也没有正确设置fopen包装器的环境中,因此我采用了上述方法(第4种,直接使用套接字) 我目前的问题是,我没有得到iCal文件,而是得到了一个301错误。但是,如果我尝试在web浏览器中访问相同的文件(通过相同的URL),它就可以正常工作 编辑: 我添加了一些日志记录,下面是它的结果:从PHP服务器端远程访问文件会得到301而不是文件,该怎么办?,php,http-status-code-301,Php,Http Status Code 301,编辑:答案在标记答案的注释中 我目前正在更新移动网站上的一些关键组件。该网站使用来自不同服务器的数据来显示学生时间表。最近,另一个网站(我对其没有任何控制权)受到了一次重大检修,我现在自然不得不更新移动网站 我试图做的是访问一个iCal文件并解析它。由于我正在处理的站点运行在一个既没有curl库也没有正确设置fopen包装器的环境中,因此我采用了上述方法(第4种,直接使用套接字) 我目前的问题是,我没有得到iCal文件,而是得到了一个301错误。但是,如果我尝试在web浏览器中访问相同的文件(通
-------------
Querying url:
https://someUrl/schema/ri654Q055ZQZ60QbQ0ygnQ70cWny067Z0109Zx4h0Z7o525Y407Q.ics
Response:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.8
Date: Sun, 11 Aug 2013 14:08:36 GMT
Content-Type: text/html
Content-Length: 184
Connection: close
Location:
https://someUrl/schema/ri654Q055ZQZ60QbQ0ygnQ70cWny067Z0109Zx4h0Z7o525Y407Q.ics
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.2.8</center>
</body>
</html>
Redirect url found: https://someUrl/schema/ri654Q055ZQZ60QbQ0ygnQ70cWny067Z0109Zx4h0Z7o525Y407Q.ics
是一个永久重定向,而不是错误
您的代码必须遵循该重定向才能访问资源
例如,返回301以将用户重定向到
您可以在第2行看到301响应,后面是位置标题,它告诉web浏览器改为去哪里
可能发生的情况是,在这次大修期间,他们将资源转移到了另一个位置。为了不破坏任何用户的书签或日历,他们使用了301重定向,以便客户端自动从新位置获取资源。我找到了位置行,但它包含与我最初用于获取文件的URL相同的URL。这是我得到的回应:
HTTP/1.1 301永久移动
服务器:nginx/1.2.9日期:Sun,2013年8月11日00:59:51 GMT内容类型:文本/html内容长度:184连接:关闭位置:https://***/schema/ri654Q055ZQZ60QbQ0ygnQ70cWny067Z0109Zx4h0Z7o525Y407Q.ics 301永久移动301永久移动
nginx/1.2.9
@user1131180并且是位置之后的内容:
正是您想要的内容吸引人?这包括https,以及编辑的域前面是否有www。准确地获取位置后面的内容,并尝试取回该位置。例如,如果我通过http获取你的URL,它会给我一个301重定向到https资源(一个简单的谷歌搜索显示你发布了一个带有完整URL的粘贴站)。不用担心URL是公开的,我试图让评论看起来更好,但完全搞砸了。给定的新位置与初始位置相同,脚本陷入循环中。我将用我正在使用的代码更新问题。@user1131180在$fp=fsocketopen
行上,将fsocketopen
更改为$fp=fsocketopen('ssl://'.$host,$port…
当您使用fsocketopen连接到HTTPS或使用ssl的端口时,需要将ssl://放在主机名前面,以便fsocketopen可以处理ssl握手。如果重定向与您的请求相同,则服务器可能配置错误。您应该与他们联系。(但请注意,这不是httpvshttps
或wwwvs[无www]
的区别。
function getRemoteFile($url)
{
error_log("------------- \r\nQuerying url: " . $url, 3, "error_log.log");
// get the host name and url path
$parsedUrl = parse_url($url);
$host = $parsedUrl['host'];
if (isset($parsedUrl['path'])) {
$path = $parsedUrl['path'];
} else {
// the url is pointing to the host like http://www.mysite.com
$path = '/';
}
if (isset($parsedUrl['query'])) {
$path .= '?' . $parsedUrl['query'];
}
if (isset($parsedUrl['port'])) {
$port = $parsedUrl['port'];
} else {
// most sites use port 80
// but we want port 443 because we are using https
error_log("Using port 443\r\n" . $url, 3, "error_log.log");
$port = 443;
}
$timeout = 10;
$response = '';
// connect to the remote server
$fp = fsockopen($host, $port, $errno, $errstr, $timeout );
if( !$fp ) {
echo "Cannot retrieve $url";
} else {
$payload = "GET $path HTTP/1.0\r\n" .
"Host: $host\r\n" .
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" .
"Accept: */*\r\n" .
"Accept-Language: sv-SE,sv;q=0.8,en-us,en;q=0.3\r\n" .
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
"Referer: https://$host\r\n\r\n";
error_log("\nPAYLOAD: " . $payload, 3, "error_log.log");
// send the necessary headers to get the file
fputs($fp, $payload);
// retrieve the response from the remote server
while ( $line = stream_socket_recvfrom( $fp, 4096 ) ) {
$response .= $line;
}
fclose( $fp );
// naively find location redirect
$location_pos = strpos($response, "Location:");
if($location_pos){
$location_pos += 10;
$new_url = substr($response, $location_pos, strpos($response, "\r\n\r\n") - $location_pos);
error_log("\nRedirect url found: " . $new_url, 3, "error_log.log");
}else{
//log the response
error_log($response, 3, "error_log.log");
}
// strip the headers
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos + 4);
}
// return the file content
return $response;
}
$ curl -I http://google.com/
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sun, 11 Aug 2013 01:25:34 GMT
Expires: Tue, 10 Sep 2013 01:25:34 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic