//采集公共类
class CurlApi extends HomeController {
/**
* reqeust_by_curl
* 通过Curl请求
*
* @param string $url 请求地址,必选
* @param array/string POST或PUT的数据参数
* @param string $mothod 请求类型 POST GET PUT DELETE
* @param string $cookie_dir cookie 保存目录,默认保存到系统临时目录
* @param string $cookie_file 已有 cookie 文件时,使用该 cookie 文件里的 cookie 访问
* @param string $http_version HTTP 版本,支持 1.0, 1.1, 2.0, 2TLS, 2_PRIOR_KNOWLEDGE ,默认 1.1
* @param string $referer 来源地址
* @param array $headers 自定义头部信息
* @param boolean $nobody 是否返回消息主体
* @param string $encoding 编码类型 identity, deflate, gzip
* @param int $request_timeout 请求超时时间
* @param int $response_timeout 响应超时时间
* @param string $user_agent 用户浏览器类型
* @return mixed array
* int httpcode 响应代码
* array cookies 数组保存的获取到的 cookie
* string cookie_file 本次保存的 cookie 文件
* string effective_url 跳转地址
* string header 响应头
* string body 响应主体
*/
public function request_by_curl()
{
// 获得传入的所有参数的数组
$args_arr = func_get_args();
// 数组分配变量,添加arg_头缀
extract($args_arr[0], EXTR_PREFIX_ALL, 'arg');
// 请求地址,必填,否则返回false;
$url = $arg_url;
if(empty($url)) {
return false;
}
isset($arg_data) ? $data = $arg_data : $data = null;
isset($arg_method) ? $method = $arg_method : $method = 'GET';
isset($arg_cookie_dir) ? $cookie_dir = $arg_cookie_dir : $cookie_dir = sys_get_temp_dir();
isset($arg_cookie_file) ? $cookie_file = $arg_cookie_file : $cookie_file = null;
isset($arg_http_version) ? $http_version = $arg_http_version : $http_version = '1.1';
isset($arg_referer) ? $referer = $arg_referer : $referer = null;
isset($arg_headers) ? $headers = $arg_headers : $headers = null;
isset($arg_nobody) ? $nobody = $arg_nobody : $nobody = false;
isset($arg_encoding) ? $encoding = $arg_encoding : $encoding = null;
isset($arg_request_timeout) ? $request_timeout = $arg_request_timeout : $request_timeout = 30;
isset($arg_response_timeout) ? $response_timeout = $arg_response_timeout : $response_timeout = 30;
isset($arg_user_agent) ? $user_agent = $arg_user_agent : $user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0;)';
$ch = curl_init();
// 请求地址
curl_setopt($ch, CURLOPT_URL, $url);
// cookie处理
if(empty($cookie_file)) {
$cookie_file = tempnam($cookie_dir, 'shuke_');
}
$size = strlen(file_get_contents($cookie_file));
if($size <= 0) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
} else {
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
}
// SSL HTTPS
if(stripos($url, 'https://') !== false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
}
// 设置同时返回响应头信息
curl_setopt($ch, CURLOPT_HEADER, TRUE);
// 设置不返回消息主体
if($nobody) {
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
}
// 设置返回值给变量,不直接显示结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// 请求超时时间
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
// 响应超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, $response_timeout);
// User-Agent
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
// Accept-Encoding
if(!empty($encoding)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Accept-Encoding: '.$encoding);
curl_setopt($ch, CURLOPT_ENCODING, $encoding);
}
// 自定义header
if(!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// 来源Referer
if(!empty($referer)) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
// 抓取跳转信息
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// HTTP版本选择,默认HTTP 1.1
switch($http_version) {
case '1.0':
curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0);
break;
case '2.0':
curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_2_0);
break;
case '2TLS':
curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_2TLS);
break;
case '2_PRIOR_KNOWLEDGE':
curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
break;
default:
curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);
}
//拼接参数
$query_string = '';
if(is_array($data)) {
foreach ($data as $key => $val) {
$query_string .= urlencode($key).'='.urlencode($val).'&';
}
$query_string = substr($query_string, 0, -1);
} else {
$query_string = $data;
}
//请求方法,默认GET
switch($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
}
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
// 折分响应头和主体
list($header, $body) = explode("\r\n\r\n", $response, 2);
// 解析响应头 cookie
preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches);
$cookie = $matches[1];
$response_arr = array('httpcode' => $httpcode, 'cookies'=> $cookie, 'cookie_file'=> $cookie_file, 'effective_url' => $effective_url, 'header' => $header, 'body' => $body);
return $response_arr;
}
}
简单调用GET请求:
$curlGet = new CurlApi;
//GET请求,响应结果存入$result变量
$result = $curlGet->request_by_curl(array('url' => 'http://www.shukeiot.cn/api'));
//取得cookie_file存放目录,下次请求可以带着cookie
if($result['httpcode'] == 200) {
$cookie_file = $result['cookie_file'];
}
//模拟登陆, $arr_params参数根据需要选择即可,最简只需要传入url
$arr_params = array(
// 请求地址,支持 SSL
'url'=>$this->login_index,
// 请求方法 GET, POST, PUT, DELETE
'method' => 'POST',
// 传递的数据,可传 null, array, string
'data' => array(
'username' => $username,
'password' => $password
),
// 已有 cookie 文件时,使用该 cookie 文件里的 cookie 访问
'cookie_file' => $cookie_file,
);
$login_result = $curlGet->request_by_curl($arr_params);