公众号调用接口
public function check_server()
{
if (isset($_GET['echostr'])) {
$this->valid();
} else {
$this->responseMsg();
$this->setMenu();
}
}
一、第一次验证公众号
private function valid()
{
$echoStr = $_GET["echostr"];
if ($this->checkSignature()) {
header('content-type:text');
echo $echoStr;
exit;
} else {
echo $echoStr . '+++' . WXTOKEN;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = WXTOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
二、公众号相关操作
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();
$msgType = $postObj->MsgType;
$event = $postObj->Event;
$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>";
$contentStr = "默认回复的文本";
switch ($msgType) {
case "event":
if ($event == "subscribe") {
$contentStr = '<a data-miniprogram-appid="小程序appid" data-miniprogram-path="小程序路径" data-miniprogram-nickname="小程序名可不填" data-miniprogram-type="text" href="">用户关注公众号返回信息</a>';
}
break;
case "text":
if (strpos($keyword, '退款') !== false || strpos($keyword, '退钱') !== false) {
$contentStr = '检测到对应文本返回';
} elseif (strpos($keyword, '客服') !== false || strpos($keyword, '投诉') !== false || strpos($keyword, '订单') !== false) {
$contentStr = '检测到对应文本返回';
}
break;
}
if ($event == "CLICK" && $postObj->EventKey == "business_cooperation") {
$contentStr = '点击自定义菜单返回';
}
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
} else {
echo "";
exit;
}
}
private function setMenu()
{
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->getToken();
$data = '{
"button":[
{
"type":"miniprogram", //跳转小程序
"name":"菜单名1",
"url":"备用地址",
"appid":"小程序appid",
"pagepath":"小程序页面地址"
},
{
"type":"miniprogram", //跳转小程序
"name":"菜单名2",
"url":"备用地址",
"appid":"小程序appid",
"pagepath":"小程序页面地址"
},
{
"type":"click", //点击回复
"name":"菜单名3",
"key":"business_cooperation" //回复消息时接收的KEY自定义
}
]
}';
$resArr = $this->httpRequest($url, $data);
var_dump($resArr);
}
public function getToken($type = 0)
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
return $access_token;
}
private function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function httpRequest($url, $data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$res = curl_exec($ch);
if (substr($res, 0, 3) == pack("CCC", 0xEF, 0xBB, 0xBF)) $res = substr($res, 3);
$resArr = json_decode($res, true);
curl_close($ch);
return $resArr;
}
公众号技术谈论
|