Note that there are some explanatory texts on larger screens.

plurals
  1. PORemote file access from PHP server side gives 301 instead of file, what to do?
    text
    copied!<p>EDIT: the answer is in the comments to the marked answer.</p> <p>I am currently working with updating a few key components on a mobile web site. The site uses data from a different server to display student schedules. Recently this other site (over which I have zero control) was subject to a major overhaul and naturally I now have to update the mobile web site. </p> <p>What I am trying to do is to access an iCal file and parse it. Since the site I am working on runs in an environment that does not have the curl-library nor have fopen wrappers properly set up I have resorted to the method described <a href="http://www.php-mysql-tutorial.com/wikis/php-tutorial/reading-a-remote-file-using-php.aspx" rel="nofollow">here</a> (number 4, using a socket directly).</p> <p>My current issue is that instead of getting the iCal-file I get a 301 error. However, if I attempt to access the same file (via the same URL) in a web browser it works just fine.</p> <p>EDIT: I added a bit of logging and here is what came out of it:</p> <pre><code>------------- 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 &lt;html&gt; &lt;head&gt;&lt;title&gt;301 Moved Permanently&lt;/title&gt;&lt;/head&gt; &lt;body bgcolor="white"&gt; &lt;center&gt;&lt;h1&gt;301 Moved Permanently&lt;/h1&gt;&lt;/center&gt; &lt;hr&gt;&lt;center&gt;nginx/1.2.8&lt;/center&gt; &lt;/body&gt; &lt;/html&gt; Redirect url found: https://someUrl/schema/ri654Q055ZQZ60QbQ0ygnQ70cWny067Z0109Zx4h0Z7o525Y407Q.ics </code></pre> <p>The new location I am getting is identical to the original one. </p> <p>This is the code used:</p> <pre><code>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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload