微信公众平台上实现笑话是个很受欢迎的功能,经常有人问怎么做笑话,方倍工作室提供了接口100里面有个笑话API的。
笑话接口为:
013. 笑话
http://apix.sinaapp.com/joke/?appkey=trialuser
返回如下:
"有三个准球迷,国籍是中、日、韩,死后同时到了上帝的面前,上帝对他们说:“按照惯例你们每人可以问一个问题。”日本球迷最先问:“日本何时拿到大力神杯?”上帝说:“还要50年!”日本球迷流着眼泪离开了。韩国球迷问了同样的问题,上帝说还要100年,韩国球迷同样哭着走了。中国球迷也问上帝:“大力神何日落户中国?”上帝走上前紧紧地握住他的手,上帝哭了。"
"老师问:“铁放在外面,常和空气接触,就会生锈。那么金呢?”学生答:“会被偷。”"
"妻子比平时晚回来了两小时,丈夫大发雷霆:“干什么去了,怎么晚了两个小时!” “实在对不起。不过也没有办法,车站的自动扶梯坏了,我正站在扶梯上,只好一直等到故障完全排除。” “什么?你说你在扶梯上站了两个小时?你真是个傻瓜!你干嘛不坐着等呢?!”"
实现很简单,直接调用笑话接口即可
<?php /* 方倍工作室 CopyRight 2013 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"]; if($this->checkSignature()){ echo $echoStr; exit; } } 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; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "text": $resultStr = $this->receiveText($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = ""; break; } echo $resultStr; }else { echo ""; exit; } } private function receiveText($object) { $keyword = trim($object->Content); $url = "http://apix.sinaapp.com/joke/?appkey=trialuser"; $output = file_get_contents($url); $contentStr = json_decode($output, true); if (is_array($contentStr)){ $resultStr = $this->transmitNews($object, $contentStr); }else{ $resultStr = $this->transmitText($object, $contentStr); } return $resultStr; } private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "欢迎关注方倍工作室"; break; default: break; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } 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>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $resultStr; } 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>"; $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item)); return $resultStr; } } ?>
时间: 2024-10-27 22:22:24