PHP & Others

url주소로 파일내용 읽어오기 [allow_url_fopen = off ]

페이지 정보

본문

<?
$url = "URL설정";

 

$info = parse_url($url);

$send = "POST ".$info[path]." HTTP/1.1\r\n"
      . "Host: ".$info[host]."\r\n"
      . "Content-type: application/x-www-form-urlencoded\r\n"
      . "Content-length: ".strlen($info[query])."\r\n"
      . "Connection: close\r\n\r\n".$info[query];

 

$fp = @fsockopen($info[host], 80);
fputs($fp, $send);

 

$start = false;
$return = "";
while(!feof($fp)) {
   $tmp = fgets($fp, 1024);
   if($start == true) $return .= $tmp;
   if($tmp == "\r\n") $start = true;
}
@fclose($fp);

 

echo $return;
?>

 

 

or

allow_url_fopen = off 일때, 외부 파일 읽어오기

 

function get_url_fsockopen( $url ) {
$URL_parsed = parse_url($url);

$host = $URL_parsed["host"];
$port = $URL_parsed["port"];
if ($port==0)
$port = 80;

$path = $URL_parsed["path"];
if ($URL_parsed["query"] != "")
$path .= "?".$URL_parsed["query"];

$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";

$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 128);
if ( $body )
$in .= $s;
if ( $s == "\r\n" )
$body = true;
}

fclose($fp);
echo $in;
}
}

 

//참고 : phpcafe 

관련자료

등록된 댓글이 없습니다.
Today's proverb
행복은 뭘까? 우리와 함께하는 것들. 숨쉬는 공기, 나무, 하늘, 가족, 친구. 이에 대한 고마움은 스쳐지나가기가 쉽다. 행복은 우리와 함께하는 것들의 가치를 아는 것이다. (신현림)