Computer/PHP, ASP
PHP - HTTP GET Request 구현
Gu Youn
2007. 8. 9. 18:45
1. 개념 fsockopen 함수를 사용하여 구현한다. 2. 소스 DEFINE("CONTENT_TYPE", "multipart/form-data; boundary="); DEFINE("CRLF", "\r\n"); DEFINE("CONTENT_DISPOSITION", "Content-Disposition: form-data; name=\"%s\""); DEFINE("FILE_NAME_PLACE_HOLDER", "; filename=\"%s\""); DEFINE("CONTENT_TYPE_PLACE_HOLDER", "Content-Type: %s\r\n\r\n"); DEFINE("CONTENT_LENGTH", "Content-Length: %d\r\n"); DEFINE("BOUNDARY", "---------------------------" . "020603111835686"); function http_form_get_send($host, $port, $url, $result_len) { $headers = array( "GET $url HTTP/1.0" . CRLF, "Accept: */*" . CRLF, "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)" . CRLF, "Host: $host" . CRLF, "Connection: Close" . CRLF, ); $sock = fsockopen($host, $port); // send headers for($i = 0;$i < count($headers);$i++) { fputs($sock, $headers[$i]); } // send end of header fputs($sock, CRLF); while(!feof($sock)) { $s = fgets($sock, 1024); if($s) { // skip headers if($s[0] == "\n" || $s[0] == "\r") break; } else break; } $result = fread($sock, $result_len); fclose($sock); return $result; } 3. 사용 예 $data = "?ownerid=blue"; $data .= "&ticket=5230315"; $data .= "&from=016"; $data .= "&to_number=016"; $data .= "&message=hohoho"; $result = http_form_get_send("ipager.test.co.kr", "80", "/sendsms/" . $data, 1024) ; print "result : " . $result; |
4. 소스파일