需求场景:
q:小李想要去看电影,但又不知道看什么?这就需要看看最近有哪些好看的电影呢?
a:打开公众号,关注即可查看最新热门电影。
<?php/*** wechat php test*///define your tokenheader("Content-type:text/html;charset=utf-8");define("TOKEN", "weixin");配置header 头,数据类型,token验证来源是否拥有权限。$res = json_decode(file_get_contents("https://api./cgi-bin/token?grant_type=client_credential&appid=wx1d7c6fcd613e43b3&secret=8a20b454c4d24bef8ed69168c0c2aa67"),true);// 请求解码token获取access_token$access_token = $res['access_token'];// var_dump($access_token);exit;$wechatObj = new wechatCallbackapiTest();//$wechatObj->valid();$wechatObj->responseMsg();// 测试apiclass 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 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);$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 = '你长得真好看';$cont= '共检测到';$appkey = "94a0805ac661a31196e2e3e50c0df7d5";$secret= "utuC5iUZWr8xdjXy_PtMPhKeEbRtXTno";$pic = $postObj->PicUrl;$api = "/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 $message;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 = "/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 = "/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./telematics/v3/movie?qt=hot_movie&location={$jd},{$wd}&ak=RpDvOBdte5dyLXYRpAzc0EDHgugAPDuI&output=json"),true);//var_dump($res['result']['movie']);exit;$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,'测试标题','描述描述描述','/wechat/pic.jpg','/');}elseif(!empty( $keyword )){if($keyword=='天气'){$contentStr = "今天天气不错!";}elseif($keyword=='快递'){$contentStr = "快递运输中!";}else{$contentStr = "不知道你在说什么!";}$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;}}}// 创建公众号菜单function createMenu($data){$ch = curl_init();curl_setopt($ch,CURLOPT_URL,"https://api./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./cgi-bin/menu/get?".$access_token);}// 删除菜单function deleteMenu(){return file_get_contents("https://api./cgi-bin/menu/delete?".$access_token);}// 自定义菜单的json格式数据,需要用单引号包含在里面$data = '{"button": [{"type": "click", "name": "56565歌曲", "key": "V1001_TODAY_MUSIC"}, {"name": "菜单", "sub_button": [{"type": "view", "name": "搜索", "url": "/?userid=337883018&wfr=wx_profile"}, {"type": "view", "name": "视频", "url": "/"}, {"type": "click", "name": "赞一1下我们", "key": "V1001_GOOD"}]}]}';echo createMenu($data);?>