微信公众号之调用api查询热门电影

时间:2021-03-04 12:28:22
<?php
/**
* wechat php test
*/


//define your token
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'];
//var_dump($access_token);exit;




$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{

public function valid()
{

$echoStr = $_GET["echostr"];

//valid signature , option
if($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
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 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;
}
}
}




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);


?>