模拟提交登录表单:
$ post_url = 'http://www.xxxx'; //登录表单提交地址
$post = "username=$account&password=$password&seccodeverify=$verifyCode";//表单提交的数据(根据表单字段名和用户输入决定)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ post_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //提交方式为post
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch);
curl_close($ch);
php CURL模拟登陆+获取cookie
https://blog.csdn.net/qq_30151745/article/details/86589513
/*
* 模拟post请求
*/
function post_curl($url, $params=[], $headers=[]){
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , http_build_query($params));
curl_setopt( $ch , CURLOPT_URL , $url );
$response = curl_exec( $ch );
if ($response === FALSE) {
return false;
}
curl_close( $ch );
return $response;
}
$re = post_curl($http, $data, 1);
// 解析HTTP数据流
list($header, $body) = explode("\r\n\r\n", $re);
// 解析COOKIE
preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
//请求的时候headers 带上cookie就可以了
$cookie = explode(';', $matches[1])[0];
如此一来 就可以模拟登陆得到 本次登陆 cookie值
如何利用上面的cookie 去访问网页,去post数据,去get页面代码的函数↓↓↓↓↓↓↓↓↓↓↓↓这是好文
https://www.cnblogs.com/linguanh/p/4292316.html
function post($url,$post_data,$location = 0,$reffer = null,$origin = null,$host = null){
$post_data = is_array($post_data)?http_build_query($post_data):$post_data;
//产生一个urlencode之后的请求字符串,因为我们post,传送给网页的数据都是经过处理,一般是urlencode编码后才发送的
$header = array( //头部信息,上面的函数已说明
'Accept:*/*',
'Accept-Charset:text/html,application/xhtml+xml,application/xml;q=0.7,*;q=0.3',
'Accept-Encoding:gzip,deflate,sdch',
'Accept-Language:zh-CN,zh;q=0.8',
'Connection:keep-alive',
'Content-Type:application/x-www-form-urlencoded',
//'CLIENT-IP:'.$ip,
//'X-FORWARDED-FOR:'.$ip,
);
//下面的都是头部信息的设置,请根据他们的变量名字,对应上面函数所说明
if($host){
$header = array_merge_recursive($header,array("Host:".$host));
}
else if($this->option["host"]){
$header = array_merge_recursive($header,array("Host:".$this->option["host"]));
}
if($origin){
$header = array_merge_recursive($header,array("Origin:".$origin));
}
else{
$header = array_merge_recursive($header,array("Origin:".$url));
}
if($reffer){
$header = array_merge_recursive($header,array("Referer:".$reffer));
}
else{
$header = array_merge_recursive($header,array("Referer:".$url));
}
$curl = curl_init(); //这里并没有带参数初始化
curl_setopt($curl, CURLOPT_URL, $url);//这里传入url
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//对认证证书来源的检查,不开启次功能
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//从证书中检测 SSL 加密算法
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
//模拟用户使用的浏览器,自己设置,我的是"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自动设置referer
curl_setopt($curl, CURLOPT_POST, 1);//开启post
curl_setopt($curl, CURLOPT_ENCODING, "gzip" );
//HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。
//我上面设置的是*/*
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//要传送的数据
//curl_setopt($curl, CURLOPT_COOKIE, $this->cookies);//以变量形式发送cookie,我这里没用它,文件保险点
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); //存cookie的文件名,
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); //发送
curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制,防止死循环
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Curl error: ' . curl_error ( $curl );exit();
}
curl_close($curl);
这个$tmpInfo 就是请求返回的数据 包含请求头和请求体
得到的是字符串类型的请求头请求体 没有我想要的json数组 怎么办? ↓↓↓↓↓↓↓↓↓↓↓↓ 解决问题点
curl 返回http头信息处理;curl返回header头和body处理
处理方式:用curl自带的curl_getinfo()方法获取头的长度,然后使用substr来分割字符串
//对返回的结果进行字符串处理
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $headerSize);
$body = substr($result, $headerSize);
}
return $body;
注意 要在 curl_close($curl);
之前该字符串,因为这是curl自带的方法,close掉了还调用毛…
此时得到了字符串类型的请求体 酷似json数组,其实虚有其表,必须转换成json数组,但是有坑
直接用json_encode转换 会出现问题 会变成下面这种格式
array(2) {
[0]=>
object(stdClass)#2 (4) {
["id"]=> string(1)"1"
["name"]=> string(9)"张雪梅"
["age"]=> string(2)"27"
object(stdClass)#3 (4) { 这个就说明转换的json字符串转为对象而非数组,请看下面的红色背景字
["subject"]=>string(24) "计算机科学与技术"
}
[1]=>
["id"]=> string(1)"2"
["name"]=> string(9)"张沛霖"
["age"]=> string(2)"21"
["subject"]=> string(12) "软件工程"
}
}
这不是我想要的json数组 返回的结果是 object 而非 array 参考两篇文章更加走心
php json字符串转为数组或对象
怎么把php字符串转换成json格式?
其实转成了这种方式 也可以遍历出来
foreach($students as $obj){
echo "姓名:".$obj->name."年龄:".$obj->age."专业:".$obj->subject."<br/>";
}
最终我选择使用了 json_decode()
json_decode($your_json,true) //得到的是 array
于是可以对这个虚有其表的json字符串为所欲为。