서버로 Request 요청 후 DB 인풋
Server Side/PHP2016. 11. 20. 06:54
//보낼 데이터 $data = array( 'im' =--> 'bunkering', 'kong' => 222, 'tak' => 'heater' ); //POST요청을 위하여 im=bunkering&kong=2&... 형태로 변경 while(list($n,$v) = each($data)){ $send_data[] = "$n=$v"; } $send_data = implode('&', $send_data); //url 파싱 $url = "http://yourDomain/test.php"; $url = parse_url($url); $host = $url['host']; $path = $url['path']; //소켓 오픈 $fp = fsockopen($host, 80, $errno, $errstr, 10.0); if (!is_resource($fp)) { echo "not connect host : errno=$errno,errstr=$errstr"; exit; } fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: " . strlen($send_data) . "\r\n"); fputs($fp, "Connection:close" . "\r\n\r\n"); fputs($fp, $send_data); //반환값 받기 $result = ''; while(!feof($fp)) { $result .= fgets($fp, 128); } fclose($fp); //헤더와 컨텐츠(웹페이지에 표시되는 내용) 구분 $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; //결과 출력 echo $header; echo $content;
'Server Side > PHP' 카테고리의 다른 글
PHP + JSON 으로 rest API 만들기 (0) | 2016.12.11 |
---|---|
PHP json 인코딩 처리 (0) | 2016.12.02 |
php interface 사용방법 (0) | 2016.10.01 |
PHP simplexml_load_file 로 XML 파싱 하기 (0) | 2016.09.08 |
브라우저 캐시 남기지 않기 (0) | 2016.08.31 |