php微信開發(fā)之關(guān)注事件
更新時間:2018年06月14日 11:29:23 作者:qiphon3650
這篇文章主要為大家詳細(xì)介紹了php微信開發(fā)之關(guān)注事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了php微信關(guān)注事件的具體代碼,供大家參考,具體內(nèi)容如下
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$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)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$type = $postObj->MsgType;
$customevent = $postObj->Event;
$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($type=="event" and $customrevent=="subscribe"){
$contentStr = "感謝你的關(guān)注\n回復(fù)1查看聯(lián)系方式\n回復(fù)2查看最新資訊\n回復(fù)3查看法律文書";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
if(!empty( $keyword ))
{
$msgType = "text";
if($keyword=="1"){
$contentStr = "qiphon";}
if($keyword=="2"){
$contentStr = "test 。";}
if($keyword=="3"){
$contentStr = "test333";}
$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;
}
}
}
?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Yii中srbac權(quán)限擴展模塊工作原理與用法分析
這篇文章主要介紹了Yii中srbac權(quán)限擴展模塊工作原理與用法,結(jié)合實例形式分析了srbac模塊的原理及權(quán)限操作的相關(guān)技巧,需要的朋友可以參考下2016-07-07
合并ThinkPHP配置文件以消除代碼冗余的實現(xiàn)方法
這篇文章主要介紹了合并ThinkPHP配置文件以消除代碼冗余的實現(xiàn)方法,非常實用的技巧,需要的朋友可以參考下2014-07-07
PHP中使用register_shutdown_function函數(shù)截獲fatal error示例
這篇文章主要介紹了PHP中使用register_shutdown_function函數(shù)截獲fatal error示例,需要的朋友可以參考下2015-04-04
Yii+MYSQL鎖表防止并發(fā)情況下重復(fù)數(shù)據(jù)的方法
這篇文章主要介紹了Yii+MYSQL鎖表防止并發(fā)情況下重復(fù)數(shù)據(jù)的方法,分析了MySQL讀鎖定、寫鎖定及Yii實現(xiàn)MySQL讀寫鎖定的相關(guān)技巧,需要的朋友可以參考下2016-07-07

