How does PHP implement WeChat simulation login, get user list and mass message?

Source: Internet
Author: User
Tags preg
This article mainly introduces the PHP implementation of the simulation landing, access to the user list and the bulk of the message function, combined with specific examples of the PHP interface to analyze the relevant calls and operation skills, the need for friends can refer to the next

This paper describes the PHP implementation of the simulation login, access to the user list and mass message function. Share to everyone for your reference, as follows:


<?phpheader (' content-type:text/html; Charset=utf-8 ');/* Call method Simple description: $arr = Array (' account ' = ' public Platform accounts ', ' Password ' = ' password '), $w = new Weixin ($arr);p Rint_r ($w->getalluserinfo ());//Get all user information $w->getuserinfo ($groupid, $fakeid) ;//Get all user information, if the default grouping, then $groupid 0$w->sendmessage (' mass content '); Mass to all users $w->sendmessage (' mass content ', $userId);  Mass to a specific user, where the $userid is the user's Fakeid, array way to pass */class Weixin {public $userFakeid;//All Fans Fakeid private $_account;//user name Private $_password;//password private $url;//requested URL private $send _data;//submitted data private $getHeader = 0;//Show header information private  $token;//Public account token private $host = ' mp.weixin.qq.com ';//host private $origin = ' https://mp.weixin.qq.com ';  private $referer;//reference address private $cookie; Private $pageSize = 100000;//per page of users (for all users) private $userAgent = ' mozilla/5.0 (Windows NT 6.1; WOW64;  rv:23.0) gecko/20100101 firefox/23.0 ';    Public function construct ($options) {$this->_account = isset ($options ["Account"])? $options [' Account ']: '; $this->_pasSword = isset ($options [' Password '])? $options [' Password ']: ';  $this->login ();    }//Login Private Function login () {$url = ' https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ';  $this->send_data = Array (' username ' = = $this->_account, ' pwd ' = = MD5 ($this->_password), ' F '    = ' json ');    $this->referer = "https://mp.weixin.qq.com/";    $this->getheader = 1;    $result = explode ("\ n", $this->curlpost ($url));      foreach ($result as $key = = $value) {$value = Trim ($value);      if (Preg_match ('/token= (\d+)/I ', $value, $match)) {//Get token $this->token = Trim ($match [1]);            } if (Preg_match ('/"ret":(. *)/I ', $value, $match)) {//Get token switch ($match [1]) {case-1:          Die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' system error ')));          Case-2: Die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' account number or password error ')]);        Case-3:    Die (UrlDecode (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' =>urlencode ("password Error"))));          Case-4: Die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' does not exist for this account '));          Case-5: Die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' access restricted '));          Case-6: Die (Json_encode (' status ' =>1, ' errcode ' = ' + ' $match [1], ' msg ' = ' = ' need to enter a verification code ')]);          Case-7: Die (Json_encode (' status ' =>1, ' Errcode ' $match [1], ' msg ' + = ' This account has a private number that is not available for public platform login '));          Case-8: Die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' mailbox already exists '));          Case-32:die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' Auth code input error ')]);          Case-200:die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' = ' due to frequent submission of false information, the account is denied login ")); Case-94:die (Json_encode (Array (' status ' =>1, ' ErrcoDe ' = $match [1], ' msg ' = ' Please login using email '));          Case 10:die (Json_encode (' status ' =>1, ' errcode ' = ' $match [1], ' msg ' = ' = ' The public meeting number has expired and can no longer be logged in using ') ');            Case 0: $this->userfakeid = $this->getuserfakeid ();        Break }} if (Preg_match ('/^set-cookie:[\s]+ ([^=]+) = ([^;] +)/I ', $value, $match) {//Get cookie $this->cookie. = $match [1]. ' = '. $match [2]. ';      '; }}}//single message private function Send ($fakeid, $content) {$url = ' https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-    RESPONSE&AMP;LANG=ZH_CN '; $this->send_data = Array (' type ' = = 1, ' f ' = ' json ', ' action ' = ' sync ', ' content    ' = ' $content, ' tofakeid ' + $fakeid, ' token ' + $this->token, ' ajax ' = 1, '; $this->referer = ' https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token= '. $this->token. ' &fromfakeid= '. $fakeid. ' &msgid=&source=&count=20&t=wxm-singlechat&aMP;LANG=ZH_CN ';  return $this->curlpost ($url);      }//Mass message public function sendMessage ($content = ', $userId = ') {if (Is_array ($userId) &&!empty ($userId)) {        foreach ($userId as $v) {$json = Json_decode ($this->send ($v, $content));        if ($json->ret!=0) {$errUser [] = $v; }}}else{foreach ($this->userfakeid as $v) {$json = Json_decode ($this->send ($v [' Fakeid '], $conte        NT));        if ($json->ret!=0) {$errUser [] = $v [' Fakeid '];    }}}//Total number of sending users $count = count ($this->userfakeid);    Number of failed send users $errCount = count ($errUser);    Number of successful users sent $succeCount = $count-$errCount; $data = Array (' status ' =>0, ' count ' = = $count, ' succecount ' = $succeCount, ' errcount ' = $errCo    UNT, ' erruser ' = $errUser);  Return Json_encode ($data); }//Get all user information public function Getalluserinfo () {foreach ($this->userfakeid as $v) {$info [] = $this->getuseri Nfo$v [' GroupID '], $v [' Fakeid ']);  } return $info; }//Get user Information public function getuserinfo ($groupId, $fakeId) {$url = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=a    jax-getcontactinfo&lang=zh_cn&fakeid={$fakeId} ";    $this->getheader = 0; $this->referer = ' https://mp.weixin.qq.com/cgi-bin/contactmanagepage?token= '. $this->token. ' &t=wxm-friend&lang=zh_cn&pagesize= '. $this->pagesize. '    &pageidx=0&type=0&groupid= '. $groupId;    $this->send_data = Array (' token ' = = $this->token, ' Ajax ' =>1);    $message _opt = $this->curlpost ($url);  return $message _opt;    }//Get all Users Fakeid private function Getuserfakeid () {ini_set (' max_execution_time ', 600);    $pageSize = 1000000; $this->referer = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_cn&token={$this    token} "; $url = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize={$pageSize}&pageidx=0& Type=0&groupid=0&token={$this-&GT;TOKEN}&AMP;LANG=ZH_CN ";    $user = $this->vget ($url);    $preg = "/\" id\ ":(\d+), \" nick_name\ "/";    Preg_match_all ($preg, $user, $b);    $i = 0;      foreach ($b [1] as $v) {$arr [$i] [' fakeid '] = $v;      $arr [$i] [' groupid '] = 0;    $i + +;  } return $arr; The Post method for/** * Curl Analog Login * @param $url request address * @param $header analog Headre header information * @return JSON */Private Functio n Curlpost ($url) {$header = array (' accept:*/* ', ' accept-charset:gbk,utf-8;q=0.7,*;q=0.3 ', ' Accept-enco       Ding:gzip,deflate,sdch ', ' accept-language:zh-cn,zh;q=0.8 ', ' connection:keep-alive ', ' Host: '. $this->host,    ' Origin: '. $this->origin, ' Referer: '. $this->referer, ' x-requested-with:xmlhttprequest '); $curl = Curl_init (); Start a Curl session curl_setopt ($curl, Curlopt_url, $url); Address to access curl_setopt ($curl, Curlopt_httpheader, $header); Sets an array of HTTP header fields curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Check the source of the certificate Curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists from the certificate curl_setopt ($curl, curlopt_useragent, $this->useragent); Simulates user-used browser curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $this->send_data); Post-Submitted packet curl_setopt ($curl, Curlopt_cookie, $this->cookie); Read stored cookie information curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, $this->getheader); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $result = curl_exec ($curl); Perform a Curl session curl_close ($curl);  Turn off Curl return $result;        } Private Function Vget ($url) {///Simulate get content function $header = Array (' Accept: */* ', ' connection:keep-alive ', ' Host:mp.weixin.qq.com ', ' Referer: '. $this->referer, ' x-requested-with:xmlhttprequest ');    $useragent = ' mozilla/5.0 (Windows NT 6.1; WOW64;    rv:23.0) gecko/20100101 firefox/23.0 '; $curl = Curl_init (); Start a Curl session curl_setopt ($curl, Curlopt_url, $url); Address to access curl_setopt ($curl, Curlopt_httpheader, $header); Sets an array of HTTP header fields curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists curl_setopt ($curl, Curlopt_useragent, $useragent) from the certificate; Simulates user-used browser curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_httpget, 1); Send a regular GET request curl_setopt ($curl, Curlopt_cookie, $this->cookie); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, $this->getheader); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl); Perform action if (Curl_errno ($curl)) {//echo ' ERrno '. Curl_error ($curl); } curl_close ($curl); Turn off the curl session return $tmpInfo; Return Data}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.