-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwx_api.php
More file actions
117 lines (105 loc) · 5.16 KB
/
wx_api.php
File metadata and controls
117 lines (105 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
//define your token
define("TOKEN", "wx_token");//微信token
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
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
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$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';
$contentStr = '欢迎订阅本公众号\n查询天气请回复天气\n人脸识别请上传图片';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else if($postObj->MsgType == 'image'){
$msgType = 'text';
$pic= $postObj->PicUrl;
$api="http://apicn.faceplusplus.com/v2/detection/detect?api_key=596d66398a9724f7216cd08523b29d20&api_secret=sTKcDiAXBJYR0JEqXWPUe5yTHpwTg91w&url={$pic}&attribute=gender,age,race";
$rs=json_decode(file_get_contents($api),true);
$str = '共检测到'.count($rs['face'])."个人,分别是\n";
foreach ($rs['face'] as $f) {
$str = $str . $f['attribute']['race']['value'] ." ". $f['attribute']['gender']['value']." ".$f['attribute']['age']['value']."\n";
}
$contentStr = $str;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else if ($postObj->MsgType == 'location') {
$msgType = 'text';
$api="http://api.map.baidu.com/telematics/v3/local?location={$postObj->Location_Y},{$postObj->Location_X}&keyWord=%E5%AD%A6%E6%A0%A1&output=json&ak=07c8d27bbe614cfeec7383b722c6ccb4";
$rs=json_decode(file_get_contents($api),true);
for ($contentStr='',$i=0; $i <3&&$i<$rs['count']; $i++) {
$contentStr = $contentStr.$rs['pointList'][$i]['name']." ".$rs['pointList'][$i]['address']." 距离".$rs['pointList'][$i]['distance']."米\n";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else if(!empty( $keyword ))
{
$msgType = "text";
if ($keyword =='天气') {
$api="http://api.map.baidu.com/telematics/v3/weather?location=%E6%B1%95%E5%A4%B4&output=json&ak=07c8d27bbe614cfeec7383b722c6ccb4";
$rs=json_decode(file_get_contents($api),true);
$date=$rs['results'][0]['weather_data'];
for ($i=0,$contentStr=''; $i <count($date); $i++) {
$contentStr = $contentStr.$date[$i]['date']." ".$date[$i]['weather']." ".$date[$i]['wind']." ".$date[$i]['temperature']."\n";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
$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()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>