Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dano Stuporstar

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

php ffl_HttpGet()

If your server has allow_url_fopen disabled, use this as a workaround for file() ... hell use it anyway and get the http headers as a bonus.
   1  
   2  /**
   3   FUNCTION: ffl_HttpGet()
   4   * Perform a HTTP Get Request.
   5   *
   6   * ffl_HttpGet uses fsockopen() to request a given URL via HTTP
   7   * 1.0 GET and returns a three element array.  On success, array
   8   * key 'body' contains the body of the request's reply and key
   9   * 'header' contains the reply's headers.  On error, the keys
  10   * returned are 'errornumber' and 'errorstring' from
  11   * fsockopen()'s third and fourth arguments.  In either case,
  12   * key 'url' contains an array such as returned from parse_url()
  13   * after the input url has been massaged a bit.
  14   *
  15   * {@source }
  16   *
  17   * @param string $url URL to fetch.
  18   * @param boolean $followRedirects Optionally follow 
  19   * 'location:' in header, default true.
  20   * @return array 'header', 'body', 'url' OR 'errorstring',
  21   * 'errornumber', 'url'.
  22   */
  23  function ffl_HttpGet( $url, $followRedirects=true ) {
  24      $url_parsed = parse_url($url);
  25      if ( empty($url_parsed['scheme']) ) {
  26          $url_parsed = parse_url('http://'.$url);
  27      }
  28      $rtn['url'] = $url_parsed;
  29  
  30      $port = $url_parsed["port"];
  31      if ( !$port ) {
  32          $port = 80;
  33      }
  34      $rtn['url']['port'] = $port;
  35      
  36      $path = $url_parsed["path"];
  37      if ( empty($path) ) {
  38              $path="/";
  39      }
  40      if ( !empty($url_parsed["query"]) ) {
  41          $path .= "?".$url_parsed["query"];
  42      }
  43      $rtn['url']['path'] = $path;
  44  
  45      $host = $url_parsed["host"];
  46      $foundBody = false;
  47  
  48      $out = "GET $path HTTP/1.0\r\n";
  49      $out .= "Host: $host\r\n";
  50      $out .= "Connection: Close\r\n\r\n";
  51  
  52      if ( !$fp = @fsockopen($host, $port, $errno, $errstr, 30) ) {
  53          $rtn['errornumber'] = $errno;
  54          $rtn['errorstring'] = $errstr;
  55          return $rtn;
  56      }
  57      fwrite($fp, $out);
  58      while (!feof($fp)) {
  59          $s = fgets($fp, 128);
  60          if ( $s == "\r\n" ) {
  61              $foundBody = true;
  62              continue;
  63          }
  64          if ( $foundBody ) {
  65              $body .= $s;
  66          } else {
  67              if ( ($followRedirects) && (stristr($s, "location:") != false) ) {
  68                  $redirect = preg_replace("/location:/i", "", $s);
  69                  return ffl_HttpGet( trim($redirect) );
  70              }
  71              $header .= $s;
  72          }
  73      }
  74      fclose($fp);
  75  
  76      $rtn['header'] = trim($header);
  77      $rtn['body'] = trim($body);
  78      return $rtn;
  79  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS