这几天碰到这样一个需求,就想到了用curl模拟登入,之前没用过curl干模拟登入的事,现在遇到了,就想一定要掌握把curl学会,过程花了我2天时间,虽然我干了一年php了,解决这个问题真的花了我2天时间,也许看别人的文章的时候别人说什么多轻松之类的,我自己摸索还是感觉有点费劲的。
下面我就把我最后测试成功的代码贴出来吧,一般登入是很好模拟的,现在回头总结,也就是那么回事了。下面帖代码
<span style=”font-size:18px;”><?php
//模拟微信登入
$cookie_file = tempnam(‘./temp’,‘cookie’);
$login_url = ‘https://mp.weixin.qq.com/cgi-bin/login’;
$data = ‘f=json&imgcode=&pwd=ae4e792c8c289cc8e390c86c99ea249b&username=97653723@qq.com’;
$ch = curl_init($login_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_REFERER,‘https://mp.weixin.qq.com’);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$content = curl_exec($ch);
curl_close($ch);
$newurl = json_decode($content,1);
$newurl = $newurl[‘redirect_url’];
//获取登入后页面的源码
$go_url = ‘https://mp.weixin.qq.com’.$newurl;
$ch = curl_init($go_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($ch);
//var_dump(curl_error($ch));
print_r($content);
curl_close($ch);
?></span>
评论回复