微信公众平台开发(95) 世界杯赛程
2014年巴西世界杯(英语:2014 FIFA World Cup)是第20届世界杯足球赛。比赛于2014年6月12日至7月13日在南美洲国家巴西境内12座城市中的12座球场内举行。这是继1950年巴西世界杯之后世界杯第二次在巴西举行,也是继1978年阿根廷世界杯之后世界杯第五次在南美洲举行。
巴西世界杯共有32支球队参赛。除去东道主巴西自动获得参赛资格以外,其他31个国家需通过参加2011年6月开始的预选赛获得参赛资格。巴西世界杯期间,总共在巴西境内举办共计64场比赛角逐出冠军。同时,巴西世界杯是首届运用门线技术的世界杯。
实现代码
<?php /* 方倍工作室 CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixin"); $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" =>"//images0.cnblogs.com/i/340216/201406/111304544204656.jpg", "Url" =>"http://url.cn/RInu1v"); 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" =>"//images0.cnblogs.com/i/340216/201406/111304544204656.jpg", "Url" =>"http://url.cn/RInu1v"); }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 = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $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> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> </xml>"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item)); return $result; } private function logger($log_content) { if(isset($_SERVER['HTTP_BAE_ENV_APPID'])){ //BAE require_once "BaeLog.class.php"; $logger = BaeLog::getInstance(); $logger ->logDebug($log_content); }else if(isset($_SERVER['HTTP_APPNAME'])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL $max_size = 10000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } } ?>
效果图
代码下载
https://files.cnblogs.com/txw1958/2014worldcup.zip
====================================================================
方倍工作室微信公众平台账号关注方法:
1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
3. 使用微信扫描下面的二维码