1.建立测试号
1.1 首先需要写服务器连接测试程序,建立连接通路
//demo.php
define("TOKEN", "weixin");
//接收随机字符串
$echoStr = $_GET["echostr"];
//进行用户数字签名验证
if(checkSignature()){
//如果成功,则返回接收到的随机字符串
echo $echoStr;
//退出
exit;
}
function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
1.2.申请测试号
测试号申请连接
正式号和测试号所有开发路径是一样的,但是接口有诸多限制。
url: 填写demo.php的路径地址【此路径地址外部要是可访问的】
token:就是第一步中代码中的token
2.1 根据用户输入回复各种类型消息
建立通路之后,用户每输入一次消息,微信都会发一个post请求到【1.2】配置的url当中,处理post请求按照模板【各种消息模板】返回即可
//接收用户端发送过来的XML数据
$postStr = @file_get_contents('php://input');
//extract post data
//判断XML数据是否为空
if (!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);
//通过simplexml进行xml解析
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//手机端
$fromUsername = $postObj->FromUserName;
//微信的公众平台
$toUsername = $postObj->ToUserName;
//接收用户发送的关键词
$keyword = trim($postObj->Content);
//接收用户消息类型
$msgType = $postObj->MsgType;
//定义$longitude与$latitude接收用户发送的经纬度信息
$latitude = $postObj->Location_X;
$longitude = $postObj->Location_Y;
//接收Event与EventKey参数
$event = $postObj->Event;
$eventKey = $postObj->EventKey;
//时间戳
$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>";
//音乐发送模板
$musicTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
</xml>";
//图文发送模板
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
%s
</xml>";
if($msgType=='text') {
//判断用户发送关键词是否为空
if(!empty( $keyword ))
{
if($keyword=='文本') {
//回复类型,如果为“text”,代表文本类型
$msgType = "text";
//回复内容
$contentStr = "您发送的是文本消息";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//把XML数据返回给手机端
echo $resultStr;
} elseif($keyword=='?' || $keyword=='?') {
//定义回复类型
$msgType=='text';
//回复内容
$contentStr = "【1】特种服务号码\n【2】通讯服务号码\n【3】银行服务号码\n您可以通过输入【】方括号的编号获取内容哦!";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//返回数据到微信客户端
echo $resultStr;
} elseif ($keyword=='1') {
//定义回复类型
$msgType=='text';
//回复内容
$contentStr = "常用特种服务号码:\n匪警:110\n火警:119";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//返回数据到微信客户端
echo $resultStr;
} elseif ($keyword=='2') {
//定义回复类型
$msgType=='text';
//回复内容
$contentStr = "常用通讯服务号码:\n中移动:10086\n中电信:10000";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//返回数据到微信客户端
echo $resultStr;
} elseif ($keyword=='3') {
//定义回复类型
$msgType=='text';
//回复内容
$contentStr = "常用银行服务号码:\n工商银行:95588\n建设银行:95533";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//返回数据到微信客户端
echo $resultStr;
} elseif ($keyword=='音乐') {
//定义回复类型
$msgType='music';
//定义音乐标题
$title = '冰雪奇缘';
//定义音乐描述
$desc = '《冰雪奇缘》原声大碟...';
//定义音乐链接
$url = 'http://czbk888.duapp.com/music.mp3';
//定义高清音乐链接
$hqurl = 'http://czbk888.duapp.com/music.mp3';
//格式化字符串
$resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType, $title, $desc, $url, $hqurl);
//返回XML数据到微信客户端
echo $resultStr;
} elseif ($keyword=='图文') {
//定义回复类型
$msgType='news';
//定义返回图文数量
$count = 4;
//组装Articles节点信息
$str = '<Articles>';
for($i=1;$i<=$count;$i++) {
$str .= "<item>
<Title><![CDATA[微信开发教程{$i}]]></Title>
<Description><![CDATA[传智播客微信开发教程...]]></Description>
<PicUrl><![CDATA[http://czbk888.duapp.com/images/{$i}.jpg]]></PicUrl>
<Url><![CDATA[http://www.itcast.cn]]></Url>
</item>";
}
$str .= '</Articles>';
//格式化字符串
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType, $count, $str);
//输出XML数据并返回到微信客户端
echo $resultStr;
} else {
//定义回复类型
$msgType='text';
//定义url链接操作
$url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={$keyword}";
//模拟发送http中的get请求
$str = file_get_contents($url);
//格式化json字符串为对象或数组
$json = json_decode($str);
//定义回复内容
$contentStr = $json->content;
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//返回数据到微信客户端
echo $resultStr;
}
}else{
echo "Input something...";
}
}
}