[Function] fsockopen을 이용한 웹페이지, 이미지 읽기
페이지 정보
작성자 MintState 댓글 0건 조회 24,810회 작성일 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]]]] )
또다른 방법
외부파일이나 외부이미지를 가져올때 좋습니다. 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); } ?>
|
댓글목록
등록된 댓글이 없습니다.