1、目的
完成在微信公众号中群发消息。这里只是完成简单的文字发送。也可以发送语音图片等,只是发送数据格式不同而已,下面有链接,可以查询数据类型的数据发送格式。
2、群发短信的流程
获取测试公众账号(有账号的可以不用测试账号,不过正式的账号限制比较多)用户关注上面的公众账号通过appid和appsecret获取我们的access_token通过access_token群发短信
3、获取测试公众账号 + 关注公众号
1)、公众测试账号获取
访问上面的连接,选择“接口测试号申请”获得直接打开http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index通过微信客户端扫码登录即可登录。
登录完即可获取到一个测试公众账号的信息。主要有appId和appsecret两个参数,这将唯一标示一个公众号,并且需要将他们作为参数获取用户的信息。、
2)、配置接口信息
这一步可以参照微信接入说明,该页提供一个php的实例下载,很简单基本上修改一下自定义的TOKEN就好了,然后把验证页面放到自己的服务器上。
这里我提供我做的一个例子:
准备资源:
域名+空间(我的是sae空间+万网域名)、仅作验证的php文件
域名指向的空间根目录我创建了一个wx_sample.php
wx_sample.php
<?php/*** wechat php test*///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTest{public function valid(){$echoStr = $_GET["echostr"];//valid signature , optionif($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 dataif (!empty($postStr)){/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,the best way is to check the validity of xml by yourself */libxml_disable_entity_loader(true);$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(){// you must define TOKEN by yourselfif (!defined("TOKEN")) {throw new Exception('TOKEN is not defined!');}$signature = $_GET["signature"];$timestamp = $_GET["timestamp"];$nonce = $_GET["nonce"];$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);// use SORT_STRING rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}}?>
新闻热点
疑难解答