php curl 获取请求头与DNS解析

1 php-curl方法相关设置

具体方法在最下方的示例函数有相关编著, 这里主要描述两个小众需求

a 设置访问DNS解析
问题点: get请求网页获取返回值速度很快, 但是使用curl请求数据时, 响应速度奇慢
经排查是域名解析ip地址解析时间过长, 在curl中设置解析列表格式为

curl_setopt($ci, CURLOPT_RESOLVE, [
  "host:port:ip"//示例 api.butian.net:443:101.227.27.122
]);

b 设置返回响应头信息
问题点: 请求时需要获取响应头中的token, 组装对应token, 所以需要获取响应头信息
使用 CURLOPT_HEADER 设置项, 将响应头信息添加到返回值中
具体设置为
curl_setopt($ci, CURLOPT_HEADER, true);
对应返回值为

string(402) "HTTP/1.1 200 OK
Server: nginx
Date: Tue, 25 Aug 2020 08:48:46 GMT
Content-Type: text/html;charset=utf-8
Connection: close
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=uglau364277s7j89g2iraodj60; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Strict-Transport-Security: max-age=15768000

 相对应的格式化函数也贴一下

/**
* 将header信息格式化
* @param $header_str
* @return array
*/
public static function http_header_to_arr($header_str){
$header_list = explode(" ", $header_str);
$header_arr = [];
foreach ($header_list as $key => $value){
if(strpos($value, ':') === false){
continue;
}
list($header_key, $header_value) = explode(":", $value, 2);
$header_arr[$header_key] = trim($header_value);
}
if(isset($header_arr['Content-MD5'])){
$header_arr['md5'] = bin2hex(base64_decode($header_arr['Content-MD5']));
}
return $header_arr;
}



这里是具体的curl请求函数,已将对应的需求作为参数编写

/*
* * 发起请求并获取返回值 * @param string $url * @param string $method 请求方式 * @param array|string $requestData 请求数据 * @param array $headers 请求头 数组 * @param string $returnFormat 返回数据当 1.默认data,返回响应体 2.header,返回响应头信息 * @return string */publicstatic function request($url, $method = 'GET', $requestData = null, $headers = null, $returnFormat = 'data') { if (is_array($requestData)) { $requestData = http_build_query($requestData); } if ('GET' == strtoupper($method) && $requestData) { $url = $url.'?'.urlencode($requestData); } $headerFormat = false; if($returnFormat == 'header'){ $headerFormat = true; } $ci = curl_init(); //设置解析地址 curl_setopt($ci, CURLOPT_RESOLVE, [ "api.butian.net:443:101.227.27.122" ]); //302的也可以获取 curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ci, CURLOPT_URL, $url); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); // 不直接输出curl_setopt($ci, CURLOPT_HEADER, $headerFormat);// 启用时会将头文件的信息作为数据流输出curl_setopt($ci, CURLOPT_AUTOREFERER, $headerFormat);// 当根据Location:重定向时,自动设置header中的Referer:信息 curl_setopt($ci, CURLOPT_USERAGENT, 'FO UA V1.0'); curl_setopt($ci, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ci, CURLOPT_TIMEOUT, (60)); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // 是否验证ssl证书(http)curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);// 是否验证证书使用于此主机(http)   if ($headers) { curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); } if ('POST' == strtoupper($method)) { curl_setopt($ci, CURLOPT_POST, true); curl_setopt($ci, CURLOPT_POSTFIELDS, $requestData); } if('PUT' == strtoupper($method)){ curl_setopt ($ci, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ci, CURLOPT_POSTFIELDS, $requestData); } $ret = curl_exec($ci); //$requestHeader = curl_getinfo($ci, CURLINFO_HEADER_OUT); //查看请求头信息 //note_log($requestHeader);//return $requestHeader; $httpCode = curl_getinfo($ci,CURLINFO_HTTP_CODE); //http错误码 //判断http返回的错误码   if ($httpCode != 200 ) { $err_str = "HTTP ERR NUM: ".$httpCode . " "; die($err_str); } curl_close($ci); return $ret; }
原文地址:https://www.cnblogs.com/haizizhu/p/13561459.html