花名/ID: 天析

邮箱: 2200475850@qq.com

主攻方向: 物联网安全、WEB安全

兴趣爱好: 乒乓球、羽毛球、排球、业余无线电···

备案信息:蜀ICP备19023334号

PHP 实现模拟登陆微信公众号平台

导语:

近期我又开始重视起了自己的公众号,但是由于公众号或者是说订阅号开放的权限只能被动的回复用户消息,这带给我了极大的困扰,网上寻找到一些其他语言写的模拟登陆脚本后,我在其原有的基础上改写成了该代码片段。在这里我只放出比较麻烦的登陆部分的关键代码,剩下的部分师傅们自行完善即可!

实现第一级登陆:跳转二维码验证

// 保存cookie文件位置
$COOKIE_FILE = 'weixin.cookie';

// 公众号账户配置信息
$username = "2200475850@qq.com";
$pwd = "******";

LoginA($username, $pwd, $COOKIE_FILE);

// 登陆A:验证账户密码
function LoginA($username, $pwd, $COOKIE_FILE){

    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_COOKIEJAR => $COOKIE_FILE,
    CURLOPT_POSTFIELDS => array('username' => "$username",'pwd' => md5("$pwd"),'imgcode' => '','f' => 'json'),
    CURLOPT_HTTPHEADER => array(
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36',
        'Referer: https://mp.weixin.qq.com/'
    ),
    ));

    $response = curl_exec($curl);
    $response = json_decode($response,true);

    curl_close($curl);
    return $response;

}

实现第二级登陆:获取验证二维码

// 保存验证码位置
$QR_FILE = "data.png";

LoginB($COOKIE_FILE, $QR_FILE);

// 保存登陆二维码
function LoginB($COOKIE_FILE, $QR_FILE){

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=getqrcode&param=4300',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_COOKIEFILE => "$COOKIE_FILE",
      CURLOPT_COOKIEJAR => $COOKIE_FILE,
      CURLOPT_CUSTOMREQUEST => 'GET'
    ));
    
    $file_content = curl_exec($curl);
    curl_close($curl);
    
    // 写入二进制文件数据
    $downloaded_file = fopen($QR_FILE, 'w');
    fwrite($downloaded_file, $file_content);
    fclose($downloaded_file);

}

检查登陆状态

print_r(LoginC($COOKIE_FILE));

// 检查登陆状态
function LoginC($COOKIE_FILE){

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_COOKIEFILE => "$COOKIE_FILE",
      CURLOPT_COOKIEJAR => $COOKIE_FILE,
      CURLOPT_CUSTOMREQUEST => 'GET'
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);

    return json_decode($response, true);

}

扫描二维码验证登陆状态并保存必备参数 token

print_r(LoginD($COOKIE_FILE));

// 获取必要的参数
function LoginD($COOKIE_FILE){

    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_COOKIEFILE => "$COOKIE_FILE",
    CURLOPT_COOKIEJAR => $COOKIE_FILE,
    CURLOPT_POSTFIELDS => array('f' => "json",'ajax' => "1",'random' => 0 + mt_rand()/mt_getrandmax() * (0-1)),
    CURLOPT_HTTPHEADER => array(
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36',
        'Referer: https://mp.weixin.qq.com/'
    ),
    ));

    $response = curl_exec($curl);
    $response = json_decode($response,true);

    curl_close($curl);
    
    return $response;

}

剩下的部分师傅们可以自行进行开发,实现登陆后,获取的token参数可以根据公众号平台模拟实现订阅号向指定用户发消息等一系列功能!