2007年11月30日 星期五

[PHP] fsockopen 之簡單的實作範例

<?
  if( empty($_GET['u']) )
    return;
  class 叮叮噹噹 {
    public static function getImageContent( $src_url ){
      $host = "叮叮噹噹";
      $fp = fsockopen( "叮叮噹噹" , 80, $errno, $errstr, 30);
      if( !$fp ){
        $mseg = $errst." (".$errno.")";
        return ;
      }else{
        $host   = "叮叮噹噹";
        $target   = strstr( substr( $src_url , strlen("http://") ) , "/" );
        $filename = substr( $target , strrpos( $target , "/") );
        $filename = substr( $filename , 1 );
        $referer  = "http://叮叮噹噹/";

        $out  = "GET ".$target." HTTP/1.1\r\n";
        $out .= "Host: ".$host." \r\n";
        $out .= "Referer: ".$referer." \r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
        $content = "";
        $inheader = 1;
        while (!feof($fp)) {
          $line = fgets($fp,1024);
          if ($inheader && ( $line == "\n" || $line == "\r\n"  ) ) {
            $inheader = 0 ;
          }else{
            if ($inheader == 0) {
              $content.=$line;
            }
          }
        }
        fclose($fp);
        if( empty($content) )
          return;
        header("Content-Type: application/octet-stream");
        header('Content-Disposition: inline; filename="'.$filename.'"');
        echo $content;
      }
    }
  }
  叮叮噹噹::getImageContent( $_GET['u'] );
?>


沒有留言:

張貼留言