<?php
header("Content-type:text/html;charset=utf-8");
define("TOKEN", "weixin");
$res = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1d711fcd613e43b3&secret=8a20b454c4d24bef8ed69168c0c2a117"),true);
$access_token = $res['access_token'];
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
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($postObj->MsgType =="event"&&$postObj->Event=="subscribe"){
$msgType = 'text';
$cont = '欢迎关注';
echo sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$cont);
}elseif($postObj->MsgType =="image"){
$msgType = 'text';
$cont= '共检测到';
$appkey = "94a0805ac661a31196e2e3e50c0df7d5";
$secret = "utuC5iUZWr8xdjXy_PtMPhKeEbRtXTno";
$pic = $postObj->PicUrl;
$api = "http://apicn.faceplusplus.com/v2/detection/detect?api_key={$appkey}&api_secret={$secret}&url={$pic}&attribute=glass,pose,gender,age,race,smiling";
$rs =json_decode(file_get_contents($api),true);
$rs = $rs['face'];
$count = count($rs);
$cont = '共检测到'.$count."个人,分别是";
foreach ($rs as $f) {
$gender = $f['attribute']['gender']['value'];
if($gender=='Male'){
$gender='男性--';
}else{
$gender='女性--';
}
$age = $f['attribute']['age']['value'].'岁';
$cont = $cont.$gender.$age;
}
echo sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$cont);
}elseif($keyword=='音乐'){
$textTpl="<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>";
$msgType = 'music';
$title="mmmm";
$desc="一首好听的音乐";
$url = "http://chenkaixuan.com/wechat/YoungRisingSons-High.mp3";
$msgType = 'news';
$title="默认标题";
$desc="信息描述";
echo sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$title,$desc,$url,$url);
}
elseif($keyword=='图片消息'){
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$msgType = 'image';
$title="mmmm";
$desc="一首好听的音乐";
$url = "http://chenkaixuan.com/wechat/YoungRisingSons-High.mp3";
$msgType = 'news';
$title="默认标题";
$desc="信息描述";
echo sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$title,$desc,$url,$url);
}
elseif($postObj->MsgType =="location"){
$msgType = 'text';
$jd=$postObj->Location_Y;
$wd=$postObj->Location_X;
$res = json_decode(file_get_contents("http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location={$jd},{$wd}&ak=RpDvOBdte5dyLXYRpAzc0EDHgugAPDuI&output=json"),true);
$c='';
foreach ($res['result']['movie'] as $v) {
$c.=$v['movie_name']."\n\r";
}
$cont=$c;
echo sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$cont);
}elseif($keyword=='图文消息'){
$msgTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
echo sprintf($msgTpl, $fromUsername, $toUsername, $time, 'news',1,'测试标题','描述描述描述','http://chenkaixuan.com/wechat/pic.jpg','http://chenkaixuan.com/');
}elseif(!empty( $keyword )){
if($keyword=='天气'){
$contentStr = "今天天气不错!";
}elseif($keyword=='快递'){
$contentStr = "快递运输中!";
}else{
$contentStr = "不知道你在说什么!";
}
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
private function checkSignature()
{
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);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
function createMenu($data){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://api.weixin.qq.com/cgi-bin/menu/create?".$access_token);
curl_setopt($ch,CURLOPT_CUSTOMERQUEST,"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,CURL_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 getMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?".$access_token);
}
function deleteMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?".$access_token);
}
$data = '{
"button": [
{
"type": "click",
"name": "56565歌曲",
"key": "V1001_TODAY_MUSIC"
},
{
"name": "菜单",
"sub_button": [
{
"type": "view",
"name": "搜索",
"url": "http://weidian.com/?userid=337883018&wfr=wx_profile"
},
{
"type": "view",
"name": "视频",
"url": "http://v.qq.com/"
},
{
"type": "click",
"name": "赞一1下我们",
"key": "V1001_GOOD"
}
]
}
]
}';
echo createMenu($data);
?>