YesYo.com MintState Forums
뒤로    YesYo.com MintState BBS > Tech > PHP
검색
멤버이름    오토
비밀번호 
 

[Function] fsockopen을 이용한 웹페이지, 이미지 읽기

페이지 정보

작성자 MintState 댓글 0건 조회 24,787회 작성일 08-11-03 11:16

본문

[Function] fsockopen을 이용한 웹페이지, 이미지 읽기

외부파일이나 외부이미지를 가져올때 좋습니다. curl을 이용해도 좋지만 curl을 지원하는 계정이 얼마 없네요

fsockopen
(PHP 4, PHP 5)
fsockopen — Open Internet or Unix domain socket connection

Description
resource fsockopen ( string $target [, int $port [, int &$errno [, string &$errstr [, float $timeout]]]] )

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
   $out = "GET / HTTP/1.1\r\n";
   $out .= "Host: www.example.com\r\n";
   $out .= "Connection: Close\r\n\r\n";

   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}
?> 


<?
$host = "local.paran.com"; //접속하고자하는 도메인
$port = "80"; //일반적인 웹서버포트
$fullpath = "http://local.paran.com/inc/weather/weather_include.php"; // 검색하고자 하는 페이지의 도메인 포함 전체 주소
if(!($fp = fsockopen($host, $port, &$errno, &$errstr, 30))) // URL에 소켓 연결
{
return array(1,"소켓에러 - 검색이 중지됨", "9");
exit;
}

fputs($fp, "GET ".$fullpath." HTTP/1.0\r\n"."Host: $host:${port}\r\n"."User-Agent: Web 0.1\r\n"."\r\n"); // 서버에 URL 페이지 요청

//fputs($fp, "GET $fullpath HTTP/1.0\r\n");
//fputs($fp, "User-Agent: Mozilla/4.0\r\n");
//fputs($fp, "content-type:text/html\r\n\r\n");

while( !feof( $fp ) ) // 페이지내 모든 내용을 저장
{ 
$output .= fgets( $fp, 1024 ); 
} 

echo $output;
fclose( $fp ); // 소켓 해제
?>


<?php

function decode_header ( $str )
{
   $part = preg_split ( "/\r?\n/", $str, -1, PREG_SPLIT_NO_EMPTY );

   $out = array ();

   for ( $h = 0; $h < sizeof ( $part ); $h++ )
   {
       if ( $h != 0 )
       {
           $pos = strpos ( $part[$h], ':' );

           $k = strtolower ( str_replace ( ' ', '', substr ( $part[$h], 0, $pos ) ) );

           $v = trim ( substr ( $part[$h], ( $pos + 1 ) ) );
       }
       else
       {
           $k = 'status';

           $v = explode ( ' ', $part[$h] );

           $v = $v[1];
       }

       if ( $k == 'set-cookie' )
       {
               $out['cookies'][] = $v;
       }
       else if ( $k == 'content-type' )
       {
           if ( ( $cs = strpos ( $v, ';' ) ) !== false )
           {
               $out[$k] = substr ( $v, 0, $cs );
           }
           else
           {
               $out[$k] = $v;
           }
       }
       else
       {
           $out[$k] = $v;
       }
   }

   return $out;
}

function decode_body ( $info, $str, $eol = "\r\n" )
{
   $tmp = $str;

   $add = strlen ( $eol );

   $str = '';

   if ( isset ( $info['transfer-encoding'] ) && $info['transfer-encoding'] == 'chunked' )
   {
       do
       {
           $tmp = ltrim ( $tmp );

           $pos = strpos ( $tmp, $eol );

           $len = hexdec ( substr ( $tmp, 0, $pos ) );

           if ( isset ( $info['content-encoding'] ) )
           {
               $str .= gzinflate ( substr ( $tmp, ( $pos + $add + 10 ), $len ) );
           }
           else
           {
               $str .= substr ( $tmp, ( $pos + $add ), $len );
           }

           $tmp = substr ( $tmp, ( $len + $pos + $add ) );

           $check = trim ( $tmp );

       } while ( ! empty ( $check ) );
   }
   else if ( isset ( $info['content-encoding'] ) )
   {
       $str = gzinflate ( substr ( $tmp, 10 ) );
   }

   return $str;
}

if ( ( $io = fsockopen( "www.yahoo.com", 80, $errno, $errstr, 5 ) ) !== false )
{
   $send  = "GET / HTTP/1.1\r\n";
   $send .= "Host: www.yahoo.com\r\n";
   $send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
   $send .= "Referer: http://www.yahoo.com/\r\n";
   $send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
   $send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
   $send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
   $send .= "Accept-Language: en-us, en;q=0.50\r\n";
   $send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
   $send .= "Connection: Close\r\n\r\n";

   fputs ( $io, $send );

   $send = '';

   do
   {
       $send .= fgets ( $io, 4096 );

   } while ( strpos ( $send, "\r\n\r\n" ) === false );

   $info = decode_header ( $send );

   $send = '';

   while ( ! feof ( $io ) )
   {
       $send .= fread ( $io, 8192 );
   }

   fclose ( $io );

   $send = decode_body ( $info, $send );

   echo '<h3>Header Array</h3>';
   echo '<pre>';
   print_r ( $info );
   echo '</pre>';
   echo '<h3>Document Body</h3>';
   echo $send;

}

?>


또다른 방법
<?
$getFile = remote_read_sock("http://blogfile.paran.com/BLOG_433534/200912/1261898747_2009_ICN_airport_2.jpg");
echo $getFile['domain']."<br />";
echo $getFile['file']."<br />";
echo $getFile['basename']."<br />";
echo $getFile['url']."<br />";
echo $getFile['header']."<br />";
echo $getFile[filesize]."<br />";

function remote_read_sock($urlstr) {
	$url = parse_url2($urlstr);
	$data = "";
	$res	= @fsockopen($url[domain], 80, $strErrorNo, $strErrStr, 2);
	if($res) {
		$headerstr = "GET $urlstr HTTP/1.0\r\n"; 
		$headerstr.= "Host:{$url[domain]}:80\r\n"; 
		$headerstr.= "referer:http://$url[domain]\r\n";
		$headerstr.= "\r\n";
		fputs($res, $headerstr);
		while (!feof($res)) {
			$data.= fgets($res, 1024); 
		} 
		fclose($res);
		if (stristr($data, "Not Found") || stristr($data, "Bad Request") || stristr($data, "Forbidden"))
			return false;
	} else return false;

	$patten = "/Content\\-Length:\\s+([0-9]*)\\r\\n/i";
	preg_match($patten, $data, $match);
	if ($match) $filesize = $match[1];
	else $filesize = 0;

	$dataset = explode("\r\n\r\n", $data);

	return array("data"=>$data, "domain"=>$url[domain], "file"=>$url[file], "basename"=>$url[basename], "extension"=>$url[extension], "url"=>$urlstr, "header"=>$dataset[0], "body"=>$dataset[1], "filesize"=>$filesize);
}

function parse_url2($urlstr) {
	$url		= parse_url($urlstr);
	$domain		= str_replace("www.", "", $url[host]);									// 도메인
	$file		= substr($url[path], strrpos($url[path], "/")+1);					// 파일명
	$basename	= str_replace("%", "", substr($file, 0, strrpos($file, ".")));	// 파일명 (확장자 제외)
	if (empty($basename)) {
		$basename = $file;
		$extension = "";
	} else	$extension	= substr($file, strrpos($file,".") + 1);						// 확장자
	
	return array("url"=>$urlstr, "domain"=>$domain, "file"=>$file, "basename"=>$basename, "extension"=>$extension);
}
?>

댓글목록

등록된 댓글이 없습니다.

Total 165건 4 페이지
PHP 목록
번호 제목 글쓴이 조회 날짜
90 MintState 14997 11-03
89 MintState 14772 11-03
88 MintState 15042 11-03
87 MintState 13951 11-03
열람중 MintState 24788 11-03
85 MintState 19852 11-03
84 MintState 23678 11-03
83 MintState 15970 11-03
82 MintState 18515 11-03
81 MintState 20216 11-03
80 MintState 12751 11-03
79 MintState 13780 11-03
78 MintState 14790 11-03
77 MintState 14643 11-03
76 MintState 10666 11-03
75 MintState 15415 11-03
74 MintState 13612 11-03
73 MintState 13105 11-03
72 MintState 14708 11-03
71 MintState 10552 11-03
70 MintState 11461 11-03
69 MintState 14603 11-03
68 MintState 14571 11-03
67 MintState 12842 11-03
66 MintState 12474 11-03
게시물 검색
모바일 버전으로 보기
CopyRight ©2004 - 2024, YesYo.com MintState. ™