호스팅에서 외부페이지 내용을 가져오려면? 정보
호스팅에서 외부페이지 내용을 가져오려면?
본문
file_get_contents(), file(), fopen()등은 대부분의 호스팅에서 막아놓더라고요.
다른 서버에 http요청을 해서 내용을 받아오려면 어떻게 해야 할까요?
curl도 설치가 안 되어있던데..
다른 서버에 http요청을 해서 내용을 받아오려면 어떻게 해야 할까요?
curl도 설치가 안 되어있던데..
추천
0
0
댓글 5개

<?
// 페이지 내용 가져오기
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);
return $in; // string으로 받고싶을땐...
}
}
?>
// 페이지 내용 가져오기
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);
return $in; // string으로 받고싶을땐...
}
}
?>

fopen가 막혀 있을 땐 fsockopen가 맞습니다. 위의 블랙리스트님 소스요..

<?
// 페이지 내용 가져오기
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);
return $in; // string으로 받고싶을땐...
}
}
echo get_url_fsockopen("http://gnucomun.net");
?>
이렇게 했더니 오류가 나네요
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in ㅇㅇㅇ on line 18
Warning: fsockopen() [function.fsockopen]: unable to connect to gnucomun.net:80 in ㅇㅇㅇ on line 18
Success (0)
// 페이지 내용 가져오기
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);
return $in; // string으로 받고싶을땐...
}
}
echo get_url_fsockopen("http://gnucomun.net");
?>
이렇게 했더니 오류가 나네요
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in ㅇㅇㅇ on line 18
Warning: fsockopen() [function.fsockopen]: unable to connect to gnucomun.net:80 in ㅇㅇㅇ on line 18
Success (0)

이것도 문제가 되는지 한번 봐 보시겠어요?
<?
$host = "접속도메인";
$port = "웹서버포트";
$fullpath = "http://부르려는 페이지의 전체 경로";
if(!($fp = fsockopen($host, $port, &$errno, &$errstr, 30)))
{
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");
//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 );
?>
<?
$host = "접속도메인";
$port = "웹서버포트";
$fullpath = "http://부르려는 페이지의 전체 경로";
if(!($fp = fsockopen($host, $port, &$errno, &$errstr, 30)))
{
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");
//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 );
?>

블랙리스트님과 키스님 덕분에 해결했습니다. 정말 감사합니다.^^
키스님이 주신 소스에서는 $host에 서버 ip를 넣어야 정상작동하는 경우도 있군요.
키스님이 주신 소스에서는 $host에 서버 ip를 넣어야 정상작동하는 경우도 있군요.