사이트 내 전체검색
PHP
fsockopen() 으로 PHP 외부의 페이지 가져오기
로빈아빠
https://cmd.kr/php/808 URL이 복사되었습니다.

본문

간혹 어떤 계정에는 fget 등 외부의 파일을 읽을수 없도록 세팅된경우가 있다.

하지만 fsockopen() 은 사용할수가 있었다.

아래와 같은 방법으로 다른서버에있는 파일이나 Html 페이지를 읽어올수가 있었다.

 

<!--페이지 가져오기 시작-->
<?php
function fetch_url($theurl) {
$url_parsed = parse_url($theurl);
$host = $url_parsed["host"];
$port = $url_parsed["port"];
if($port==0) $port = 80;
$the_path = $url_parsed["path"];

if(empty($the_path)) $the_path = "/";
if(empty($host)) return false;

if($url_parsed["query"] != "") $the_path .= "?".$url_parsed["query"];
$out = "GET ".$the_path." HTTP/1.0\r\nHost: ".$host."\r\n\r\nUser-Agent: Mozilla/4.0 \r\n";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
usleep(50);
if($fp) {
socket_set_timeout($fp, 30);
fwrite($fp, $out);
$body = false;
while(!feof($fp)) {
$buffer = fgets($fp, 128);
if($body) $content .= $buffer;
if($buffer=="\r\n") $body = true;
}
fclose($fp);
}else {
return false;
}
return $content;
}

echo fetch_url("http://rtquery.naver.com/rtquery_view.php?s=nexearch&n=20");
?>

<!--페이지 가져오기 끝-->

댓글목록

등록된 댓글이 없습니다.

PHP
871 (4/18P)

Search

Copyright © Cmd 명령어 18.222.3.255