<?php
/**
* wechat php test
*/
//define your token
include_once('curl.php');
//定义一个钥匙常亮,用于服务器和微信平台连接钥匙
define("TOKEN", "weixin");
define("appID", "wxa930bb7f1fd4ecdb");
define("appsecret", "cf3d83eb7cc2d5b5cf2077549067e208");
$wechatObj = new wechatCallbackapiTest();
//验证服务器和公众平台是否连接成功
//在服务器和公众平台验证成功之后把下面代码注释掉
//$wechatObj->valid();
$wechatObj->responseMsg();
$wechatObj->createMenu();
class wechatCallbackapiTest
{ //验证
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
//验证成功 退出
if($this->checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}
}
//公众平台发送给用户的信息
public function responseMsg()
{
//get post data, May be due to the different environments
//接收用户发送给公众平台信息 ,相当于$_POST,可以接受XML格式的数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
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 */
//只解析XML数据的主体部分,防止XXE攻击
libxml_disable_entity_loader(true);
//通过simplexml解析XML数据
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//var_dump($postObj);die;
//获取手机用户的OpenID
$fromUsername = $postObj->FromUserName;
//开发者微信号
$toUsername = $postObj->ToUserName;
//发送的文本消息的关键字
$keyword = trim($postObj->Content);
//获取用户发送的消息类型
$msgtype=$postObj->MsgType;
//获取用户的事件
$event=$postObj->Event;
//获取事件的key值
$eventkey=$postObj->EventKey;
$time = time();
//echo $keyword;die;
//发送文本信息的字符串模板
$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>";
//发送图片的字符串模板
$imageTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
//发送图文消息的字符串模板
$newsTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
%s
</Articles>
</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>";
//判断用户发送过来的消息类型
//如果公众号接收的是文本消息
if($msgtype=='text'){
if(!empty( $keyword ))
{
//定义发送消息的类型
if($keyword=='图片'){
$msgType = "image";
//公众平台发送给用户的信息
$contentStr = "http://wxkflt.applinzi.com/3.jpg";
//格式化字符串
$resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}elseif($keyword=='单图文'){
$msgType='news';
$counts=1;
$contentStr="<item>
<Title><![CDATA[大家一起学习微信开发]]></Title>
<Description><![CDATA[微信开发是未来的发展方向,大家研究研究吧]]></Description>
<PicUrl><![CDATA[http://1.ltwxgzpt.applinzi.com/3.jpg]]></PicUrl>
<Url><![CDATA[http://http://1.ltwxgzpt.applinzi.com/3.jpg]]></Url>
</item>";
//格式化字符串
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
echo $resultStr;
die;
}elseif($keyword=='多图文'){
$msgType='news';
$counts=4;
$contentStr='';
for($i=2;$i<=5;$i++){
$contentStr.="<item>
<Title><![CDATA[大家一起学习微信开发]]></Title>
<Description><![CDATA[微信开发是未来的发展方向,大家研究研究吧。]]></Description>
<PicUrl><![CDATA[http://1.ltwxgzpt.applinzi.com/{$i}.jpg]]></PicUrl>
<Url><![CDATA[http://1.ltwxgzpt.applinzi.com/{$i}.jpg]]></Url>
</item>";
}
//格式化字符串
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
echo $resultStr;
die;
}elseif($keyword=='音乐'){
$msgType='music';
$title='冰雪奇缘主题曲';
$description='原声大碟';
$music_url='http://www.yyzljg.com/wechat/music.mp3';
$high_url='http://www.yyzljg.com/wechat/music.mp3';
//格式化字符串
$resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$description,$music_url,$high_url);
echo $resultStr;
die;
}elseif($keyword=='帮助'){
$msgType = "text";
//公众平台发送给用户的信息
$contentStr = "客官,回复【音乐】【多图文】【单图文】这些字,或者直接发送一张照片、一段录音、一段小视频、您现在的定位,有惊喜等着你哦,多玩伤身哦~~~";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}else{
$msgType = "text";
$curl="http://www.niurenqushi.com/app/simsimi/ajax.aspx";
$contentStr =curl($curl,array('txt'=>$keyword),'POST',false,false);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}
}else{
$msgType = "text";
$curl="http://www.niurenqushi.com/app/simsimi/ajax.aspx";
$contentStr =curl($curl,array('txt'=>$keyword),'POST',false,false);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}
}elseif($msgtype=='image'){
//定义发送消息的类型
$msgType = "text";
//公众平台发送给用户的信息
$contentStr = "您发送美女好漂亮啊~~~";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}elseif($msgtype=='voice'){
//定义发送消息的类型
$msgType = "text";
//公众平台发送给用户的信息
$contentStr = "您的声音好沧桑啊~~~啦啦啦~~";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}elseif($msgtype=='video'){
//定义发送消息的类型
$msgType = "text";
//公众平台发送给用户的信息
$contentStr = "呦,你发的什么片啊!哈哈~~~";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}elseif($msgtype=='location'){
//定义发送消息的类型
$msgType = "text";
//公众平台发送给用户的信息
$contentStr = "你怎么跑到火星啦!哈哈哈~~";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
die;
}elseif($event=='CLICK'&&$eventkey=='最新产品'){
//以图文消息回复最近的产品
$msgType='news';
//从数据库中查询最近5条数据
//连接数据库
mysql_connect('nvalglyjliip.rds.sae.sina.com.cn:25398','root','rootroot');
mysql_query('set names utf8');
mysql_query('use weixin');
$sql="select * from news limit 5";
$counts=4;
$res=mysql_query($sql);
$contentStr='';
while($row=mysql_fetch_assoc($res)){
$contentStr.="<item>
<Title><![CDATA[{$row['name']}]]></Title>
<Description><![CDATA[{$row['miaosu']}]]></Description>
<PicUrl><![CDATA[{$row['img']}]]></PicUrl>
<Url><![CDATA[http://1408phpc.applinzi.com]]></Url>
</item>";
}
//格式化字符串
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
echo $resultStr;
}elseif($event=='CLICK'&&$eventkey=='联系我们'){
//以文本消息进行回复
$msgType = "text";
//公众平台发送给用户的信息
$str="手机18665455185\n email:358288718@qq.com";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $str);
echo $resultStr;
die;
}
//首次关注自动回复
$msgType = "text";
$contentStr = "您好,欢迎首次关注!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
exit;
}
}
//菜单
public function createMenu(){
//$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa930bb7f1fd4ecdb&secret=cf3d83eb7cc2d5b5cf2077549067e208';
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.appID.'&secret='.appsecret.'';
$file=file_get_contents($url);
$arr=json_decode($file,true);
$Accesstoken=$arr['access_token'];
$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$Accesstoken;
$data='{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC",
"url":"http://www.xiami.com/"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"view",
"name":"视频",
"url":"http://v.qq.com/"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}';
$this->weixinPost($url,$data,"POST");
}
private function weixinPost($url,$data,$method){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
//4.参数如下
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);
if($method=="POST"){//5.post方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);//6.执行
if (curl_errno($ch)) {//7.如果出错
return curl_error($ch);
}
curl_close($ch);//8.关闭
return $tmpInfo;
}
//验证
private function checkSignature()
{
// you must define TOKEN by yourself
if (!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 rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
微信开发DEMO
最新推荐文章于 2024-04-23 10:11:27 发布