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 

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
우리의 꿈은, 뒤에 오는 사람들이 우리를 딛고 우리 위에서 이루게 하는 것입니다. 나는 평생을 창조적인 작업을 위해서 살아왔습니다. 누가 하라고 해서 한 것이 아니라 그것이 나의 삶 그 자체의 즐거움이었기 때문입니다. 현실을 직시하며 현재의 수준을 유지하라. 그리고 더 먼 곳을 향하는 시야를 가져라.