需求場景:
q:小李想要去看電影,但又不知道看什么?這就需要看看最近有哪些好看的電影呢?
a:打開公眾號,關注即可查看最新熱門電影。
<?php
/*** wechat php test*/
//define your token
header("Content-type:text/html;charset=utf-8");
define("TOKEN", "weixin");
配置header 頭,數據類型,token驗證來源是否擁有權限。$res = json_decode(file_get_contents("https://api.weixin.qq.com/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();
// 測試api
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 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 = "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 $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 = "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);//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,'測試標題','描述描述描述','http://chenkaixuan.com/wechat/pic.jpg','http://chenkaixuan.com/'); }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.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);
}
// 自定義菜單的json格式數據,需要用單引號包含在里面
$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);
?>
總結
以上是生活随笔為你收集整理的微信公众号开发与应用|调用api查询热门电影的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。