pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

PHP paste-bin - collaborative debugging tool View Help


Posted by MellerTime on Fri 5th Jan 14:24 (modification of post by view diff)
diff | download | new post

  1. function getRemoteFile($url)
  2. {
  3.    // get the host name and url path
  4.    $parsedUrl = parse_url($url);
  5.    $host = $parsedUrl['host'];
  6.    if (isset($parsedUrl['path'])) {
  7.       $path = $parsedUrl['path'];
  8.    } else {
  9.       // the url is pointing to the host like http://www.mysite.com
  10.       $path = '/';
  11.    }
  12.  
  13.    if (isset($parsedUrl['query'])) {
  14.       $path .= '?' . $parsedUrl['query'];
  15.    }
  16.  
  17.    if (isset($parsedUrl['port'])) {
  18.       $port = $parsedUrl['port'];
  19.    } else {
  20.       // most sites use port 80
  21.       $port = '80';
  22.    }
  23.  
  24.    $timeout = 10;
  25.    $response = '';
  26.  
  27.    // connect to the remote server
  28.    $fp = @fsockopen($host, '80', $errno, $errstr, $timeout );
  29.  
  30.    if( !$fp ) {
  31.       echo "Cannot retrieve $url";
  32.    } else {
  33.       // send the necessary headers to get the file
  34.       fputs($fp, "GET $path HTTP/1.0\r\n" .
  35.                  "Host: $host\r\n" .
  36.                  "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" .
  37.                  "Accept: */*\r\n" .
  38.                  "Accept-Language: en-us,en;q=0.5\r\n" .
  39.                  "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
  40.                  "Keep-Alive: 300\r\n" .
  41.                  "Connection: keep-alive\r\n" .
  42.                  "Referer: http://$host\r\n\r\n");
  43.  
  44.       // retrieve the response from the remote server
  45.       // we limit the length to 2 bytes each request, just to avoid using up extra bandwidth
  46.       // this would, i believe, have the effect of creating lots more little packets, though... give and take
  47.       while ( $line = fread( $fp, 2 ) ) {
  48.          $response .= $line;
  49.          
  50.          if ( substr_count($response, "<html>") > 0 ) {
  51.                 break; // break out of our while loop
  52.          }
  53.          
  54.       }
  55.  
  56.       fclose( $fp );
  57.  
  58.       // strip the headers
  59.       $pos      = strpos($response, "\r\n\r\n");
  60.       //$response = substr($response, $pos + 4);
  61.       $response = substr($response, 0, $pos);   // trim everything after our headers
  62.    }
  63.  
  64.    // return the file content
  65.    return $response;
  66. }

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings