개발/PHP

데이터 파싱 서버에서 off 일경우 // 데이터 파싱이 안될때

똘또히 2016. 11. 4. 18:30


  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을 이용하는게 나을 것 같습니다.