네이버 api작업하다 필요해서만든 소켓통신함수...
호스팅 환경이 php4다보니 이것저것 함수에 제한이 좀 있어서 검색한거에
내가 쓸만하게끔 변경함..
- GET,POST,다른포트, ssl도 그냥 되도록 만듬. http1.0이용
- http1.1에선 추가할헤더나 데이터에 이상한값문제로 보류함.
- http_build_query 가지원안되는 버젼에서는 함수를 생성하도록함.
############################################
## 소켓통신용 함수
if (!function_exists('http_build_query')) {
function http_build_query($data, $prefix = '', $sep = '', $key = '') {
$ret = array();
foreach ((array)$data as $k => $v) {
if (is_int($k) && $prefix != null) {
$k = urlencode($prefix . $k);
}
if ((!empty($key)) || ($key === 0)) {
$k = $key . '[' . urlencode($k) . ']';
}
if (is_array($v) || is_object($v)) {
array_push($ret, http_build_query($v, '', $sep, $k));
} else {
array_push($ret, $k . '=' . urlencode($v));
}
}
if (empty($sep)) {
$sep = ini_get('arg_separator.output');
}
return implode($sep, $ret);
}
}
function get_socket($url, $data,$method='GET', $referer='') {
$data = http_build_query($data);
$url = parse_url($url);
$host = $url['host'];
$path = $url['path'];
$port = $url['port'];
if(!$port){$port=80;}
//print_r($url);
//echo "DATA [ $data ]\n\n";
//exit;
// open a socket connection on port 80 - timeout: 30 sec
switch($url['scheme']){
case "http":
$fp = @fsockopen($host, $port, $errno, $errstr, 30);
break;
case "https":
$fp = @fsockopen("ssl://".$host, $port, $errno, $errstr, 30);
break;
default :
return array(
'status' => 'Error',
'error' => "https or http protocol required"
);
exit;
}
if ($fp){
if($method == "GET"){
$path .= "?".$data;
}
// send the request headers:
// GET /search?key=x&target=kin&start=1&display=10&sort=sim&query=검색어 HTTP/1.1
// Host: openapi.naver.com
// User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
// Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
// Accept-Language: ko
// Accept-Encoding: gzip, deflate
// Accept-Charset: EUC-KR,utf-8;q=0.7,*;q=0.7
// Connection: keep-alive
// Cookie: NB=GM4TSNRTGA4TONRZ; NNB=LNDCIFZVXTZE4; npic=Io1URjA1BdOJJe5XvX2cQOktCX6CjilwzS5bvqejBH6kzX/OqNAAHJgj5ERll3/OCA==
// Cache-Control: max-age=0
//fputs($fp, $method." $path HTTP/1.1\r\n");
@fputs($fp, $method." $path HTTP/1.0\r\n");
@fputs($fp, "Host: $host\r\n");
//fputs($fp, "Referer: $referer\r\n");
@fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
@fputs($fp, "Content-length: ". strlen($data) ."\r\n");
@fputs($fp, "Connection: close\r\n\r\n");
@fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
}else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
@fclose($fp);
//die($result);
// split the result header from the content
$result_post = explode("\r\n\r\n", $result, 2);
$header = isset($result_post[0]) ? $result_post[0] : '';
$content = isset($result_post[1]) ? $result_post[1] : '';
//$content = str_replace("\n","",$content);
//$content = str_replace("\r","",$content);
// return as structured array:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
,'urls' => $path
);
}
## 소켓통신용함수끝
############################################
############################################