스크립트/Php 코드 팁
호스팅시 외부URL fopen 막혀있을 때 외부파일 읽어오는 법.
Font_Charisma
2012. 1. 3. 14:54
http://www.partner114.com/bbs/board.php?bo_table=B07&wr_id=97
.htaccess 파일에 php_flag allow_url_fopen 1 내용을 추가하면 됩니다. 그외에 php.ini 내용 중 수정하고 싶은 내용을 추가해도 됩니다.
<?
$host = "local.paran.com"; //접속하고자하는 도메인
$port = "80"; //일반적인 웹서버포트
$fullpath = "http://local.paran.com/inc/weather/weather_include.php"; // 검색하고자 하는 페이지의 도메인 포함 전체 주소
if(!($fp = fsockopen($host, $port, &$errno, &$errstr, 30))) // URL에 소켓 연결
{
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"); // 서버에 URL 페이지 요청
//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 ); // 소켓 해제
?>
.htaccess 파일에 php_flag allow_url_fopen 1 내용을 추가하면 됩니다. 그외에 php.ini 내용 중 수정하고 싶은 내용을 추가해도 됩니다.
다만 요즘은 allow_url_fopen을 호스팅업체 모두 막아 두어서 적용이 안되는 경우가 많습니다. 이런 경우에는.
fsockopen 함수를 이용하면 좋습니다.
$host = "local.paran.com"; //접속하고자하는 도메인
$port = "80"; //일반적인 웹서버포트
$fullpath = "http://local.paran.com/inc/weather/weather_include.php"; // 검색하고자 하는 페이지의 도메인 포함 전체 주소
if(!($fp = fsockopen($host, $port, &$errno, &$errstr, 30))) // URL에 소켓 연결
{
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"); // 서버에 URL 페이지 요청
//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 ); // 소켓 해제
?>