티스토리 뷰

php.ini 파일에 allow_url_fopen=on으로 설정되어 있으면, URL 주소로 파일 읽어올 때 다음과 같이 처리하면 됩니다.

  1. <?php  
  2.     $fp = fopen($url"r");  
  3.    
  4.     while (!feof($fp)) {  
  5.         $retVal .= fgets($fp, 1024);  
  6.     }  
  7.    
  8.     fclose($fp);  
  9.     echo($retVal);  
  10. ?>  

하지만 allow_url_fopen=off로 설정되어 있는 경우,
일단 php.ini 파일을 수정하면 됩니다.
벗뜨~~ 웹 호스팅을 하는 경우 php.ini 파일을 직접 수정할 수 없는 경우가 있죠~
 
이런 경우의 해결책을 찾아보니 다음과 같이 socket을 이용해 URL주소를 읽어오는 방법이 있었슴다..

  1. <?php  
  2.     $url = "URL 주소";  
  3.    
  4.     $info = parse_url($url);  
  5.     $send = "POST " . $info["path"] . " HTTP/1.1\r\n"  
  6.         . "Host: " . $info["host"] . "\r\n"  
  7.         . "Content-type: application/x-www-form-urlencoded\r\n"  
  8.         . "Content-length: " . strlen($info["query"]) . "\r\n"  
  9.         . "Connection: close\r\n\r\n" . $info["query"];  
  10.    
  11.     $fp = fsockopen($info[host], 80);  
  12.     fputs($fp$send);  
  13.    
  14.     $start = false;  
  15.     $retVal = "";  
  16.    
  17.     while (!feof ($fp)) {  
  18.         $tmp = fgets($fp, 1024);  
  19.         if ($start == true) $retVal .= $tmp;  
  20.         if ($tmp == "\r\n"$start = true;  
  21.     }  
  22.    
  23.     fclose($fp);  
  24.    
  25.     echo($retVal);  
  26. ?>  


추가적으로 GET 방식 호출은 다음과 같이 할 수도 있습니다.

  1. <?php  
  2.     $url = "URL 주소";      
  3.     $info = parse_url($url);  
  4.    
  5.     $host = $info["host"];  
  6.     $port = $info["port"];  
  7.     if ($port == 0) $port = 80;  
  8.    
  9.     $path = $info["path"];  
  10.     if ($info["query"] != ""$path .= "?" . $info["query"];  
  11.    
  12.     $out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";  
  13.    
  14.     $fp = fsockopen($host$port$errno$errstr, 30);  
  15.     if (!$fp) {  
  16.         echo "$errstr ($errno) <br>\n";  
  17.     }  
  18.     else {  
  19.         fputs($fp$out);  
  20.         $start = false;  
  21.         $retVal = "";  
  22.    
  23.         while(!feof($fp)) {  
  24.             $tmp = fgets($fp, 1024);  
  25.             if ($start == true) $retVal .= $tmp;  
  26.             if ($tmp == "\r\n"$start = true;  
  27.         }  
  28.    
  29.         fclose($fp);  
  30.         echo $retVal;  
  31.     }  
  32. ?>  

기타 다른 방법으로는 .htaccess 파일을 이용하는 방법도 있더라구요..
.htaccess 파일에 다음과 같은 내용을 추가하면 된다고 합니다.
 
php_flag allow_url_fopen 1
 
하지만 위 방식보다는 fopen 대신 fsockopen을 이용하는게 나을 것 같습니다. 
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함