curl发起post,header
function call($url,$postArr,$headerArr,$isPost=false) {
//启动一个CURL会话
$ch = curl_init();
// 设置curl允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
// 证书
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false) ;
// 获取的信息以文件流的形式返回,而不是直接输出,需要设置,因为默认是0,即返回的数据无法通过变量来接收,而是直接输出了。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($isPost) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArr));
}else{
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $url .'?'. http_build_query($postArr));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
// 执行操作
$res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($res == NULL) {
curl_close($ch);
return false;
} else if($code != "200") {
curl_close($ch);
return false;
}
curl_close($ch);
return $res;
}
调用
$postArr = array(
'accid' => 'qwerty123',
'name' => 'raoxiaoya',
'mobile'=> '15919906312'
);
$headerArr= array(
'AppKey: '.$AppKey,
'Nonce: '.$Nonce,
'CurTime: '.$CurTime,
'CheckSum: '.strtolower(SHA1($AppSecret . $Nonce . $CurTime)),
'Content-Type: application/x-www-form-urlencoded;charset=utf-8'
);
call($url,$postArr,$headerArr,true);
小结
1、CURLOPT_RETURNTRANSFER
获取的信息以文件流的形式返回,需要设置为1,因为默认是0,即返回的数据无法通过变量来接收,而是直接输出了;
2、CURLOPT_POST
设置为1
3、CURLOPT_POSTFIELDS
post的字段,可以接收字符串(&连接的)或数组。
如果是一维数组的话,可以直接传;如果是超过一维了,则会报错,因此,建议先通过 http_build_query 来转义成字符串再传。
4、http_build_query
(多维)数组->(多维)关联数组->&连接的字符串->urlencode转义
示例:
$data = array('foo', 'bar', 'baz', 'boom', 'milk', 'hypertext_processor');
echo http_build_query($data) . "\n"; // 0=foo&1=bar&2=baz&3=boom&4=milk&5=hypertext_processor
$param = array('foo' => ['bar' => 'cow']);
echo http_build_query($param) . "\n"; // foo%5Bbar%5D=cow 这是什么?我们urldecode一下得到 foo[bar]=cow
5、cookie 可以放在 header 中,也可以设置在 CURLOPT_COOKIE 中
设定 HTTP 请求中"Cookie: "部分的内容。多个 cookie 用分号分隔,分号后带一个空格(例如, "fruit=apple; colour=red"
)。
6、可以使用 curl_setopt_array 来代替多次使用 curl_setopt
curl_setopt_array($ch, [
CURLOPT_POST => 1,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => $headerArr,
]);
7、有些网站故意做了限制防止被抓取,比如会响应给你302加cookie,需要再次访问并带上此cookie才能得到数据。所以此时需要访问两次。
public function requestForResult()
{
$url = 'https://www.1396r.com/shishicai/getawarddata?t=0.8737779566325454';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
$ret = curl_exec($ch);
curl_close($ch);
list($header, $body) = explode("\r\n\r\n", $ret);
preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
$cookie = explode(';', $matches[1])[0];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
一个精简的post请求
function cutl_post($url, $postArr){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArr));
$res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($res == NULL) {
curl_close($ch);
return false;
} else if($code != "200") {
curl_close($ch);
return false;
}
curl_close($ch);
return $res;
}