php transmitnews,demo.php

这段代码是一个用于微信公众号的自动回复程序,当用户订阅或提到'世界杯'、'足球'时,会回复包含2014年巴西世界杯赛程的图文消息。程序首先验证微信服务器的请求签名,然后根据接收到的消息类型(事件或文本)进行响应,如果用户触发了特定关键词,会发送赛程链接。
摘要由CSDN通过智能技术生成

/*

方倍工作室 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

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值