사이트 내 전체검색
PHP
[php] [Function] fsockopen을 이용한 웹페이지 읽기
로빈아빠
https://cmd.kr/php/504 URL이 복사되었습니다.

본문

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

fsockopen
(PHP 4, PHP 5)
[COLOR="Navy"]fsockopen ? Open Internet or Unix domain socket connection[/COLOR]

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;

}

?>

댓글목록

등록된 댓글이 없습니다.

PHP
871 (9/18P)

Search

Copyright © Cmd 명령어 3.145.176.228