//参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
function curl_request($url,$post='',$cookie='', $returnCookie=0){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
if($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
if($returnCookie){
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
$info['cookie'] = substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
}else{
return $data;
}
}
//初始化
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
$post_data = array(
"username" => "coder",
"password" => "12345"
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//执行命令
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//显示获得的数据
print_r($data);
封装:
<?PHP
/**
* CURL请求
* @param $url 请求url地址
* @param $method 请求方法 get post
* @param null $postfields post数据数组
* @param array $headers 请求header信息
* @param bool|false $debug 调试开启 默认false
* @return mixed
*/
function httpRequest($url, $method = "GET", $postfields = [], $headers = array(), $debug = false) {
$method = strtoupper($method);
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case "POST":
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
$tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
}
if (empty($headers)) {
$postfields = is_array($postfields) ? json_encode($postfields) : $postfields;
$headers = [
'Content-Type: application/json',
'Content-Length: ' . strlen($postfields),
];
}
break;
case "GET":
$params = "";
foreach ($postfields as $k => $v) {
$params .= $k . "=" . urlencode($v) . "&";
}
$params = trim($params, '&');
$url = $url . '?' . $params;
$this->data_log($url, 'get_http.log');
break;
case 'GANK':
//抓取不需要使用方法
break;
default:
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
break;
}
$ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
curl_setopt($ci, CURLOPT_URL, $url);
if ($ssl) {
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
}
//curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ci, CURLOPT_MAXREDIRS, 2); /* 指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的 */
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
curl_setopt($ci, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ci, CURLOPT_FRESH_CONNECT, 1);
/* curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
//丢包保护
for ($i = 0; $i < 10; $i++) {
$response = curl_exec($ci);
$curl_info = curl_getinfo($ci);
if (curl_errno($ci)) {
continue;
} else if ($curl_info['http_code'] !== 200) {
continue;
} else if (empty($response)) {
continue;
}
break;
}
curl_close($ci);
return $response;
//return array($http_code, $response,$requestinfo);
}
/**
* 任务挂起
* //参数1:访问的URL,参数2:响应超时时间 毫秒
*/
function curl_request($url = "http://www.baidu.com", $ms = 100) {
$curl = curl_init(); //初始化
curl_setopt($curl, CURLOPT_URL, $url); //设置抓取的url
curl_setopt($curl, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
curl_setopt($curl, CURLOPT_TIMEOUT_MS, $ms); //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
// curl_setopt($curl, CURLOPT_TIMEOUT, $s);//超时时间 秒
// curl_setopt($curl, CURLOPT_HEADER, 1);//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设置获取的信息以文件流的形式返回,而不是直接输出。
$data=curl_exec($curl);
// //丢包保护
// for ($i = 0; $i < 10; $i++) {
// $data = curl_exec($curl);
// $curl_info = curl_getinfo($curl);
// if (curl_errno($curl)) {
// continue;
// } else if ($curl_info['http_code'] !== 200) {
// continue;
// } else if (empty($data)) {
// continue;
// }
// break;
// }
curl_close($curl); //关闭URL请求
//显示获得的数据
return $data;
}
//模拟from表单提交
function form_post($url,$param) {
$delimiter = uniqid();
$post_data = buildData($param,$delimiter);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data)
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//编译请求头格式和数据流
function buildData($param,$delimiter){
$data = '';
$eol = "\r\n";
foreach ($param as $name => $content) {
$data .= "--" . $delimiter . "\r\n". 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n". $content . "\r\n";
}
$data .= "--" . $delimiter . $eol. 'Content-Disposition: form-data; name="media"; "' . "\r\n". 'Content-Type:application/octet-stream'."\r\n\r\n";
$data .= "--" . $delimiter . "--\r\n";
return $data;
}
?>
原文链接地址