Search code examples
phpfopenremote-serverpastebin

Calling file() on pastebin URL fails, but on local file or google.com it works


I'm working on a bit of PHP code that depends on a remote file which happens to be hosted on pastebin. The server I am working on has all the necessary functions enabled, as running it with FILE_URL set to http://google.com returns the expected results. I've also verified through php.ini for extra measure.

Everything should work, but it doesn't. Calling file() on a URL formed as such, http://pastebin.com/raw.php?i=<paste id here>, returns a 500 server error. Doing the same on the exact same file hosted locally or on google.com returns a reasonable result.

I have verified that the URL is set to the correct value and verified that the remote page is where I think that it is. I'm at a loss.

ini_set("allow_url_fopen", true);
// Prefer remote (up-to-date) file, fallback to local file
if( ini_get("allow_url_fopen") ){
    $file = file( FILE_URL );
}
if(!isset( $file ) || !$file ) {
    $file = file( LOCAL_FILE_PATH );
}

Solution

  • I wasn't able to test this, but you should use curl, try something like this:

    <?php
    $url = "http://pastebin.com/2ZdFcEKh";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);