模拟登陆微信公众平台

通过curl来实现模拟登陆微信公众平台

<?php  

function get_cookie(){               //获取cookie
    $url="https://mp.weixin.qq.com/cgi-bin/login";
    $username='';              //自己的公众平台账号
    $pwd='';                   //自己的公众平台密码


    $ch = curl_init($url);
    $pwd=md5($pwd);
    $cookie_file=tempnam('./item','cookie');
    $data=array(
            'username'  =>$username,
            'pwd'   =>$pwd,
            'imgcode'   =>'',
            'f'     =>'json',
    );
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');  //模拟浏览器访问
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);  //返回的信息以资源存储,不展示
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  //将cookie信息存储到制定文件夹下
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);         // 设置超时时间
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);   //设置post请求的表单值
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   //https请求,跳过证书验证
    curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com');
    $content = curl_exec($ch);
    curl_close($ch);

    $content=json_decode($content,1);
    return $content;                                 //返回内容
}


    function get_content()
    {
        $con=get_cookie();                          //获取内容
        $url="https://mp.weixin.qq.com".$con['redirect_url'];  //拼装url

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);
        curl_setopt($ch,CURLOPT_COOKIEFILE,'F:\wamp\www\csw\item\coo3722.tmp');  //使用刚才的cookie
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com');
        $content = curl_exec($ch);
        curl_close($ch);
    }

    get_content();
?>

在浏览器里用F12,查看请求地址,和请求信息。
模拟登陆的时候需要cookie,这个很关键。
有时候需要验证码才可以登陆需要在post的时候加上验证码信息。代码以后再贴。

发布了20 篇原创文章 · 获赞 4 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Csw_PHPer/article/details/50774614