Unlimited mass of all kinds of messages using the Customer service interface
- ? /*
- Author:yf
- Instruction for use: Public number wireless mass interface, use example:
- $test = new Sendallmsg ("Your AppID", "Your Appsecret");
- $test->sendmsgtoall (); Call Mass method
- Note: 1. Conditions of Use: Certification number or test number
- 2. Mass message content for graphics, text, music, etc., $data specific content Reference development document/Customer service interface
- 3. If the user is over million, you need to modify GetUserInfo (), specific reference letter development document/Get followers list
- Novice on the road, the great gods, many pointers, thank you
- */
- Interface isendallmsg{
- function GetData ($url); Curl sends a GET request
- function PostData ($url, $data); Curl sends a POST request
- function Getaccesstoken (); This method has been called in the constructor method to obtain the Access_token, note that it is saved at WX server 7200s
- function Sendmsgtoall (); Mass message method, the message sent $data can be modified by itself
- }
- Class Sendallmsg implements isendallmsg{
- Private $appId;
- Private $appSecret;
- Private $access _token;
- //
- Public function __construct ($appId, $appSecret) {
- $this->appid = $appId;
- $this->appsecret = $appSecret;
- $this->access_token = $this->getaccesstoken ();
- }
- //
- function GetData ($url) {
- $ch = Curl_init ();
- curl_setopt ($ch, Curlopt_url, $url);
- curl_setopt ($ch, Curlopt_returntransfer, 1);
- curl_setopt ($ch, Curlopt_header, 0);
- curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) ');
- curl_setopt ($ch, curlopt_encoding, ' gzip ');
- curl_setopt ($ch, Curlopt_ssl_verifypeer, false);
- $data = curl_exec ($ch);
- Curl_close ($ch);
- return $data;
- }
- //
- function PostData ($url, $data) {
- $ch = Curl_init ();
- curl_setopt ($ch, Curlopt_url, $url);
- curl_setopt ($ch, Curlopt_customrequest, "POST");
- curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
- curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
- curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) ');
- curl_setopt ($ch, curlopt_followlocation, 1);
- curl_setopt ($ch, Curlopt_autoreferer, 1);
- curl_setopt ($ch, Curlopt_postfields, $data);
- curl_setopt ($ch, Curlopt_returntransfer, true);
- $tmpInfo = curl_exec ($ch);
- if (Curl_errno ($ch)) {
- Return Curl_error ($ch);
- }
- Curl_close ($ch);
- return $tmpInfo;
- }
- //
- function Getaccesstoken () {
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". $this->appid. " &secret= ". $this->appsecret;
- $res = $this->getdata ($url);
- $jres = Json_decode ($res, true);
- $access _token = $jres [' Access_token '];
- return $access _token;
- }
- //
- Private Function GetUserInfo () {
- $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=". $this->access_token;
- $res = $this->getdata ($url);
- $jres = Json_decode ($res, true);
- Print_r ($jres);
- $userInfoList = $jres [' Data '] [' OpenID '];
- return $userInfoList;
- }
- function Sendmsgtoall () {
- $userInfoList = $this->getuserinfo ();
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=". $this->access_token;
- foreach ($userInfoList as $val) {
- $data = ' {
- "Touser": "'. $val. '",
- "Msgtype": "Text",
- "Text":
- {
- "Content": "Test it, sorry to disturb you."
- }
- }';
- $this->postdata ($url, $data);
- }
- }
- }
- $test = new Sendallmsg ("Yourappid", "Yourappsecret");
- $test->sendmsgtoall ();
- ?>
Copy Code |