微信公众号发送消息接口(群发接口)

    xiaoxiao2022-06-30  172

    //因为公众号请求发送消息接口需要认证(300RMB),所以这里选择的是测试号接口,一般来说效果差不多 //https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index首先你得去这里配置测试号,配置的源码如下 //封装成一个类,返回给微信,定义一个常量TOKEN值为weixin,用于交互微信

    define("TOKEN", "weixin");//自己定义的token 就是个通信的私钥 $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token =TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } }

    测试号配置好了后,就可以开始实现发送微信消息接口了;

    /** * @return mixed * 获取微信Access_token */ public function get_wx_access_token() { //将access_token存在session/cookie中 //如果access_token在session中并没有过期 if (isset($_SESSION['access_token']) && $_SESSION['expire_time'] > time()) { return $_SESSION['access_token']; } //如果access_token不存在或者已经过期,重新获取access_token else { //protected $appid="wx99e125***efaebe9"; //公众号APPID //protected $appsecret="14b01a0afd0ea****7c4a1a4882385c6"; //公众号秘钥 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret; $arr = $this->my_curl_wx_api($url); //存到session中 $access_token = $arr['access_token']; $_SESSION['access_token'] = $arr['access_token']; $_SESSION['expire_time'] = time() + 7200; return $access_token; } } //发送 public function service_send(){ unset($_SESSION); //获取Access_token $Access_token=$this->get_wx_access_token(); //请求地址 $url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Access_token; //组装接口Array $array=[ "touser"=>"o0roI1lyAa_8WgMNUlUOjpMlap_Y", //用户的openid "text"=>["content"=>"China is very NB!!! I love China!!!"], "msgtype"=>"text" ]; //将array->json() $postJson=json_encode($array,JSON_UNESCAPED_UNICODE); //调用url $send=$this->my_curl_wx_api($url,'POST',$postJson); var_dump($send); } /** *请求接口 */ protected function my_curl_wx_api($url, $type = 'POST', $data = array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); if($data){ curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } if(isset($data['file']) && $data['file']){ curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false); } $result = curl_exec($ch); curl_close($ch); return json_decode($result, true); }

    上面就是全部代码,喜欢给个赞鼓励鼓励~~~谢谢


    最新回复(0)