1 Star 0 Fork 544

2015phper/wemall

forked from einsqing/wemall 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
weixin.php 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
include './Public/Conf/config.php';
$sql = "select * from ".DB_PREFIX."weixin where id = '1'";
$weixin = mysql_fetch_assoc(mysql_query($sql));
define("TOKEN", "wemall");
$wechatObj = new wechatCallbackapiTest($weixin['title'] , 'http://'.$_SERVER['HTTP_HOST'].'/Public/Uploads/'.$weixin['img'] , $weixin['description'] , 'http://'.$_SERVER['HTTP_HOST'].'/App/index.php?uid=');
if (! isset ( $_GET ['echostr'] )) {
$wechatObj->responseMsg ();
} else {
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public $title;
public $picurl;
public $description;
public $appurl;
public function __construct( $title , $picurl , $description ,$appurl){
$this->title = $title;
$this->picurl = $picurl;
$this->description = $description;
$this->appurl = $appurl;
}
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;
$keyword = trim($postObj->Content);
$time = time();
$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>";
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
if(!empty( $keyword ))
{
$msgType = "news";
$Url = $this->appurl.$fromUsername;
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType, $this->title, $this->description, $this->picurl, $Url);
echo $resultStr;
}else{
if($event=="subscribe"){//首次关注
$msgType = "text";
$contentStr = "欢迎来到WeMall世界,回复'商城'即可体验WeMall商城,回复'外卖'即可体验WeMall外卖";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
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, SORT_STRING );
$tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
}
?>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/2015phper/wemall.git
git@gitee.com:2015phper/wemall.git
2015phper
wemall
wemall
master

搜索帮助