/*
方倍工作室 2014年巴西世界杯赛程
CopyRight 2014 All Rights Reserved
*/
define("TOKEN", "tokenaccesskey");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
$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){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = array();
$content[] = array("Title" =>"2014年巴西世界杯赛程","Description" =>"", "PicUrl" =>"http://images.cnitblog.com/i/340216/201406/111304544204656.jpg", "Url" =>"http://israel.sinaapp.com/worldcup/index.php?ghid=".$object->ToUserName);
break;
}
if(is_array($content)){
$result = $this->transmitNews($object, $content);
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
private function receiveText($object)
{
$keyword = trim($object->Content);
if (strstr($keyword, "世界杯") || strstr($keyword, "足球")){
$content = array();
$content[] = array("Title" =>"2014年巴西世界杯赛程","Description" =>"", "PicUrl" =>"http://images.cnitblog.com/i/340216/201406/111304544204656.jpg", "Url" =>"http://israel.sinaapp.com/worldcup/index.php?ghid=".$object->ToUserName);
}else{
$content = date("Y-m-d H:i:s",time())."\n技术支持 方倍工作室";
}
if(is_array($content)){
$result = $this->transmitNews($object, $content);
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
private function transmitText($object, $content)
{
$textTpl = "
%s
";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
private function transmitNews($object, $arr_item)
{
if(!is_array($arr_item))
return;
$itemTpl = "
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = "
%s
%s
$item_str
";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
return $result;
}
private function logger($log_content)
{
}
}
?>
一键复制
编辑
Web IDE
原始数据
按行查看
历史
这段代码是一个用于微信公众号的自动回复程序,当用户订阅或提到'世界杯'、'足球'时,会回复包含2014年巴西世界杯赛程的图文消息。程序首先验证微信服务器的请求签名,然后根据接收到的消息类型(事件或文本)进行响应,如果用户触发了特定关键词,会发送赛程链接。

被折叠的 条评论
为什么被折叠?



