易信公众平台demo代码php(含验证接口)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//易信公众平台地址https://plus.yixin.im/login,复制后后缀改为.php
define("TOKEN", "yixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$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)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDA TA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$type = $postObj->MsgType;
$customevent = $postObj->Event;
$latitude = $postObj->Location_X;
$longitude = $postObj->Location_Y;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
";
switch ($type)
{ case "event";
if ($customevent=="subscribe")
{$contentStr = "感谢你的关注\n回复1查看账号简介\n回复2查看联系方式\n回复3查看法律文书";}
break;
case "image";
$contentStr = "你的图片很棒!";
break;
case "location";
$contentStr = "你的纬度是{$latitude},经度是{$longitude},我已经锁定!";
break;
case "text";
switch($keyword)
{
case "1";
$contentStr = "易伟律师,擅长交通事故、保险纠纷、医疗损害";
break;
case "2";
$contentStr = "联系方式:汕头市金平区华乾大厦703,1341702551,email:30090032@。";
break;
case "3";
$contentStr = "31离婚协议\n32租房合同\n33交通事故协议书";
break;
default;
$contentStr ="hi";
}
break;
default;
$contentStr ="此项功能尚未开发";
}
$msgType="text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}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;
}
}
}
?>