개발/PHP
데이터 파싱 서버에서 off 일경우 // 데이터 파싱이 안될때
똘또히
2016. 11. 4. 18:30
- <?php
- $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;
- $retVal = "";
- while (!feof ($fp)) {
- $tmp = fgets($fp, 1024);
- if ($start == true) $retVal .= $tmp;
- if ($tmp == "\r\n") $start = true;
- }
- fclose($fp);
- echo($retVal);
- ?>
추가적으로 GET 방식 호출은 다음과 같이 할 수도 있습니다.
- <?php
- $url = "URL 주소";
- $info = parse_url($url);
- $host = $info["host"];
- $port = $info["port"];
- if ($port == 0) $port = 80;
- $path = $info["path"];
- if ($info["query"] != "") $path .= "?" . $info["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);
- $start = false;
- $retVal = "";
- while(!feof($fp)) {
- $tmp = fgets($fp, 1024);
- if ($start == true) $retVal .= $tmp;
- if ($tmp == "\r\n") $start = true;
- }
- fclose($fp);
- echo $retVal;
- }
- ?>
기타 다른 방법으로는 .htaccess 파일을 이용하는 방법도 있더라구요..
.htaccess 파일에 다음과 같은 내용을 추가하면 된다고 합니다.
php_flag allow_url_fopen 1