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);</p>

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

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

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