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
기적은 대개 부지런하고 열심히 그것을 좇는 사람에게 찾아간다. 앉아서 기적을 기다리는 사람에게는 영원히 찾아오지 않는다. (클레망스)