CURL 압축해제(gzip, br) 헤더와 바디 분리
자주 사용하는 CURL 함수 입니다. 아래와 같은 기능을 가집니다.
- gzip, br 압축을 풀어 줍니다.
- 파일첨부 이외에 GET, POST 파라미터
- 리퍼러 조건
- GET, POST
- 결과에서 헤더와 바디를 분리
- 쿠키값 유지
- SSL 인증서 무시
class NetworkUtil
{
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.36';
const FILE_COOKIE = '/www/cookie/file_cookie.txt';
function get_data( $http_url, $metohd='get', $post_data='', $referer='' )
{
$USER_AGENT = self::USER_AGENT;
$file_cookie = self::FILE_COOKIE;
$timeOut = 5;
$aURL = parse_url($http_url);
$HTTP_HOST = $aURL['host'];
$resolveHost = $HTTP_HOST;
// print_r($aURL); exit;
$metohd = strtoupper($metohd);
if ( $metohd == 'GET' || $metohd == 'POST' || $metohd == 'HEAD' );
else {
return false;
}
$aHeader = array();
$aHeader[] = "User-Agent: " . $USER_AGENT;
$aHeader[] = "Host: " . $HTTP_HOST;
$aHeader[] = "Accept-Encoding: gzip, deflate, br";
$aHeader[] = "Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7";
$aHeader[] = "Accept-Language: ko";
$aHeader[] = "Connection: keep-alive";
$aHeader[] = "Content-Type: application/x-www-form-urlencoded";
//multipart/form-data
// if ( $metohd == 'GET' ) {
// }
// print_r($aHeader); exit;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $http_url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER , $aHeader);
// curl_setopt($ch, CURL_HTTP_VERSION_1_1, 1);
// curl_setopt($ch, CURLOPT_USERAGENT, $USER_AGENT);
// curl_setopt($ch, CURLOPT_RESOLVE, ["$resolveHost:443:$serverIP"]);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $metohd);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if( $referer ) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if ( strlen($post_data) > 0 ) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
echo $http_url . PHP_EOL;
echo 'post: ' . $post_data . PHP_EOL;
}
curl_setopt($ch, CURLOPT_COOKIEFILE, $file_cookie); // 서버로 보내주는것
curl_setopt($ch, CURLOPT_COOKIEJAR, $file_cookie); // 서버에서 받은것 저장하기
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
$header = '';
$body = '';
if ( $http_status == 200 ) {
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$http_body = $body;
$encodeType = '';
if( stristr($header, 'Content-Encoding: gzip') ) {
$body = gzdecode($body);
$encodeType = 'gzip';
}
else if( stristr($header, 'Content-Encoding: br') ) {
$body = brotli_uncompress($body);
$encodeType = 'br';
}
}
$oRtn = (object)[];
$oRtn->response = $response;
$oRtn->http_status = $http_status;
$oRtn->header_size = $header_size;
$oRtn->http_body = $http_body;
$oRtn->header = $header;
$oRtn->body = $body;
$oRtn->encodeType = $encodeType;
return $oRtn;
}
}
* 참고할것