美文网首页微信
thinkphp结合微信实现无限群发接口

thinkphp结合微信实现无限群发接口

作者: geeooooz | 来源:发表于2017-05-31 09:38 被阅读36次

1.首先在Vendor下新建SendAllMsg文件夹,然后将SendAllMsg.php放入


SendAllMsg.php

<?php
/*
+----------------------------------------------------------------------
+ Title        : 微信公众号无线群发接口
+ Author      : wjwer
+ Version      : 1.0
+ Initial-Time : 2017.3.20
+ Last-time    : 2017.3.20 wjwer
+ Desc        :  使用说明:微信公众号无线群发接口,使用实例:
$test = new \SendAllMsg(C('WX_APPID'),C('WX_CRYPT'));
$test->sendMsgToAll();  //调用群发方法
注:1.使用条件:认证号或测试号
2.群发消息内容可为图文、文本、音乐等,$data具体内容参照微信开发文档/客服接口
3.若用户量过万,需修改getUserInfo(),具体参照信开发文档/获取关注者列表
+Ps  : 参考文档  https://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html
+----------------------------------------------------------------------
*/
interface iSendAllMsg{
    function getData($url);  //curl 发送get请求
    function postData($url,$data);  //curl 发送post请求
    function getAccessToken();  //在构造方法中已调用该方法来获取access_token,注                意它在wx服务器的保存时间7200s
    function sendMsgToAll();  //群发消息方法,发送的消息$data 可自行修改
}
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":"再来一次"
}
}';
  $this->postData($url,$data);
}
}
}
?>

在控制器中调用:

<?php
namespace Admin\Controller;
use Think\Controller;
header('Content-Type:text/html; charset= utf-8');
class CeshiController extends PublicController{
    
    public function index(){
        //微信群发
        Vendor('SendAllMsg.SendAllMsg');
        $test = new \SendAllMsg(C('WX_APPID'),C('WX_CRYPT'));
        $test->sendMsgToAll();  //调用群发方法
        
    }

}

顺带把发送其他消息的也说一下吧

微信客服接口总摘取

各消息类型所需的JSON数据包如下:
发送文本消息

    "touser":"OPENID",
    "msgtype":"text",
    "text":
    {
         "content":"Hello World"
    }
}

发送图片消息

{
    "touser":"OPENID",
    "msgtype":"image",
    "image":
    {
      "media_id":"MEDIA_ID"
    }
}

发送语音消息

{
    "touser":"OPENID",
    "msgtype":"voice",
    "voice":
    {
      "media_id":"MEDIA_ID"
    }
}

发送视频消息

{
    "touser":"OPENID",
    "msgtype":"video",
    "video":
    {
      "media_id":"MEDIA_ID",
      "thumb_media_id":"MEDIA_ID",
      "title":"TITLE",
      "description":"DESCRIPTION"
    }
}

发送音乐消息

{
    "touser":"OPENID",
    "msgtype":"music",
    "music":
    {
      "title":"MUSIC_TITLE",
      "description":"MUSIC_DESCRIPTION",
      "musicurl":"MUSIC_URL",
      "hqmusicurl":"HQ_MUSIC_URL",
      "thumb_media_id":"THUMB_MEDIA_ID" 
    }
}

发送图文消息(点击跳转到外链) 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。

{
    "touser":"OPENID",
    "msgtype":"news",
    "news":{
        "articles": [
         {
             "title":"Happy Day",
             "description":"Is Really A Happy Day",
             "url":"URL",
             "picurl":"PIC_URL"
         },
         {
             "title":"Happy Day",
             "description":"Is Really A Happy Day",
             "url":"URL",
             "picurl":"PIC_URL"
         }
         ]
    }
}

发送图文消息(点击跳转到图文消息页面) 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。

{
    "touser":"OPENID",
    "msgtype":"mpnews",
    "mpnews":
    {
         "media_id":"MEDIA_ID"
    }
}

发送卡券

{
  "touser":"OPENID", 
  "msgtype":"wxcard",
  "wxcard":{              
           "card_id":"123dsdajkasd231jhksad"        
            },
}

相关文章

网友评论

    本文标题:thinkphp结合微信实现无限群发接口

    本文链接:https://www.haomeiwen.com/subject/cumdfxtx.html