<?php
define("TOKEN", "weixin");
require '../source/class/class_core.php';
$discuz = & discuz_core::instance();
$discuz->init();
为什么我加上标红的 就不好用了呢? 无法返回微信中。???
$wechatObj = new _wechatCallback();
$wechatObj->_valid();
class _wechatCallback
{
private $items = '';
private $articleCount = 0;
private $keyword = '';
public function _valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->_checkSignature()){
echo $echoStr;
$this->_responseMsg();
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;
$this->keyword = strtolower( $postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
<FromUserName><![CDATA[".$toUsername."]]></FromUserName>
<CreateTime>".$time."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$picTpl = "<xml>
<ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
<FromUserName><![CDATA[".$toUsername."]]></FromUserName>
<CreateTime>".$time."</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%d</ArticleCount>
<Articles>
%s
</Articles>
<FuncFlag>1</FuncFlag>
</xml>";
if($this->keyword == 'hi' || $this->keyword == '您好' || $this->keyword == '你好' ||$this->keyword == 'hello2bizuser' ){
$contentStr = "输入关键字开始搜索!";//自定义欢迎回复;
echo sprintf($textTpl, $contentStr);
}else if( !empty( $this->keyword )){
$this->_search();
if($this->articleCount == 0){
$contentStr = "抱歉,没有找到与【{$this->keyword}】相关的文章,要不你更换一下关键字,可能就有结果了哦 :-) ";
echo sprintf($textTpl, $contentStr);
}else{
echo sprintf($picTpl,$this->articleCount,$this->items);
}
}
}else {
echo "";
exit;
}
}
private function _search(){
$items = '';
for($i=0;$i<5;$i++)
{
$items = $items . $this->_get_item( $shopping_cart_count, '2', '3', '4');
$this->articleCount++;
}
$this->items = $items;
}
private function _get_item($title, $description, $picUrl, $url){
if(!$description) $description = $title;
return
'
<item>
<Title><![CDATA['.$title.']]></Title>
<Discription><![CDATA['.$description.']]></Discription>
<PicUrl><![CDATA[http://'.$_SERVER['HTTP_HOST'].$picUrl.']]></PicUrl>
<Url><![CDATA[http://'.$_SERVER['HTTP_HOST'].$url.']]></Url>
</item>
';
}
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;
}
}
}
|