9 Mar 2008

Podczyt strony ze wskazanego adresu URL, fsockopen

Podczyt strony ze wskazanego adresu URL, fsockopen 

function fsopen($url) {
 if (substr($url, 0, 7) == 'http://')
  $url = substr($url, 7);

 $host = substr($url, 0, strpos($url, '/'));
 $filepath = substr($url, strpos($url, '/'));

 $fp = @fsockopen ($host, 80, $errno, $errstr, 10);

 if (!$fp) {
  $ret_value = "$errstr ($errno)";
 } else {
  $header .= 'GET '.$filepath.' HTTP/1.1'."\r\n";
  $header .= 'Host: '.$host."\r\n";
  $header .= 'Connection: close'."\r\n";
  $header .= "\r\n";

  fputs ($fp, $header);

  while(!feof($fp)) {
   $line = fgets($fp, 8192);
   if(!$start_data && preg_match("/^\r\n$/", $line)) {
    $start_data = 1;
    continue;
   }
   if ($start_data) {
    $ret_value .= $line;
   }
  }

  fclose ($fp);
 }
 return $ret_value;
}