微信公众平台开发应用(天气预报、股票查询、手机归属查询、在线听音乐、翻译、成绩查询功能)
原文: http://www.cnblogs.com/imaker/p/5491433.html
1、xml(信息返回用扩展语言XML来传递值)
$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; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; //加载图文模版 $picTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> </Articles> <FuncFlag>1</FuncFlag> </xml> "; $music ="<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[TITLE]]></Title> <Description><![CDATA[DESCRIPTION]]></Description> <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl> <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl> </Music> <FuncFlag>0</FuncFlag> </xml>";
2、当粉丝关注你的时候,在其微信会自动回复一条欢迎的图文信息
if(trim($postObj->MsgType) == "event" and trim($postObj->Event) == "subscribe")//判断是否是新关注 { /*$msgType = "text"; $contentStr = "您好,欢迎您关注“生活助手”"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;*/ $msgType = "news"; $title = "生活助手,予你方便"; $data = date(‘Y-m-d‘); $desription = "您好,欢迎您关注“生活助手” \n\n 【1】天气查询;发送“地名+1空格+天气(北京 天气)”;\n【2】中英翻译;发送“内容+1空格+翻译(时间 翻译)”; \n【3】股票查询;发送“股票名称+1空格+股票(上证指数 股票)”;\n【4】手机归属地查询;发送“手机号+1空格+归属(13800013800 归属)”;\n【5】微信笑话;发送“0+1空格+笑话(0 笑话)”;\n【6】身份证信息查询;发送“身份证号+1空格+身份(111122221552212 身份)”; \n【7】在线音乐:发送“点歌+音乐名字(点歌光辉岁月)”;\n【8】新闻:发送“任何字体+1空格+新闻(新闻 新闻)”;\n【9】成绩查询:发送“任何字+1空格+成绩(xx 成绩)”;\n【10】返回菜单;发送“任何字(xx)”;"; $image = "http://t03.pic.sogou.com/3625bc2e740c3c50.jpg"; $turl = "http://lkwebperson.duapp.com/"; $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl); echo $resultStr; }
3、字符截取函数
$str = mb_substr($keyword,-2,2,"UTF-8");//从倒数第二位开始截取,取后两位 $str_key = mb_substr($keyword,0,-2,"UTF-8");
4、翻译功能
if($str == ‘翻译‘ && !empty($str_key)){ $msgType = "text"; $contentStr = language($str_key); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }
function language($value){ $value = urlencode($value); $qurl = ‘http://fanyi.youdao.com/openapi.do?keyfrom=robot1&key=925703417&type=data&doctype=json&version=1.1&q=‘.$value; $f = new SaeFetchurl(); $content = $f->fetch($qurl); $data =date(‘Y-m-d‘); $sina = json_decode($content,true); $errorcode = $sina[‘errorCode‘]; $phonetic = $sina[‘basic‘][‘phonetic‘]; $explains = $sina[‘basic‘][‘explains‘][‘0‘]; $interpret = $sina[‘basic‘][‘explains‘][‘1‘]; $interprets = $sina[‘basic‘][‘explains‘][‘2‘]; $trans = ‘‘; if (isset($errorcode)){ switch ($errorcode){ case 0: $trans = $sina[‘translation‘][‘0‘]; break; case 20: $trans = ‘要翻译的文本过长‘; break; case 30: $trans = ‘无法进行有效的翻译‘; break; case 40: $trans = ‘不支持的语言类型‘; break; case 50: $trans = ‘无效的key‘; break; default: $trans = ‘出现异常‘; break; } } return " 翻译为:".$trans."\n 读音为:".$phonetic."\n 说明为:".$explain."\n 阐释为:".$interpret."\n 解释为:".$interprets."\n 当前时间为:".$data; }
5、天气预报
if($str ==‘天气‘ && !empty($str_key)){ $msgType = "text"; $post_data = array(); $post_data[‘city‘] = $str_key; $post_data[‘submit‘] = "submit"; $url=‘http://search.weather.com.cn/wap/search.php‘; $o=""; foreach ($post_data as $k=>$v){ $o.= "$k=".urlencode($v)."&"; } $post_data=substr($o,0,-1); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($ch); curl_close($ch); $result=explode( ‘/‘,$result); $result=explode(‘.‘,$result[‘5‘]); $citynum = $result[‘0‘]; $weatherurl = "http://m.weather.com.cn/data/".$citynum.".html"; $weatherjson = file_get_contents($weatherurl); $weatherarray = json_decode($weatherjson,true); $weatherinfo = $weatherarray[‘weatherinfo‘]; $contentTpl = "#这里是%s#(%s) %s%s %s时发布的天气预报: 今天天气:%s %s,%s 穿衣指数:%s 紫外线指数:%s 洗车指数:%s 明天天气:%s %s,%s 后天天气:%s %s,%s"; $contentStr = sprintf($contentTpl,$weatherinfo[‘city‘],$weatherinfo[‘city_en‘],$weatherinfo[‘date_y‘],$weatherinfo[‘week‘],$weatherinfo[‘fchh‘],$weatherinfo[‘temp1‘],$weatherinfo[‘weather1‘],$weatherinfo[‘wind1‘],$weatherinfo[‘index_d‘],$weatherinfo[‘index_uv‘],$weatherinfo[‘index_xc‘],$weatherinfo[‘temp2‘],$weatherinfo[‘weather2‘],$weatherinfo[‘wind2‘],$weatherinfo[‘temp3‘],$weatherinfo[‘weather3‘],$weatherinfo[‘wind3‘]); $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); echo $resultStr; }
6、在线音乐
if(substr($keyword,0,6) == "点歌"){ $entityName = trim(substr($keyword,6,strlen($keyword))); if ($entityName == ""){ $contentStr = "发送“点歌”加上歌名,如“点歌最炫民族风”"; $resultStr = transmitText($object, $contentStr, $funcFlag); return $resultStr; } $apihost = "http://api2.sinaapp.com/"; $apimethod = "search/music/?"; $apiparams = array(‘appkey‘=>"0020120430", ‘appsecert‘=>"fa6095e113cd28fd", ‘reqtype‘=>"music"); $apikeyword = "&keyword=".urlencode($entityName); $apicallurl = $apihost.$apimethod.http_build_query($apiparams).$apikeyword; $api2str = file_get_contents($apicallurl); $api2json = json_decode($api2str, true); $musicUrl = $api2json[‘music‘][‘hqmusicurl‘]; if ($musicUrl == ""){ $contentStr = "没有找到音乐,可能不是歌名或者检索失败,请换首歌试试!"; $resultStr = transmitText($object, $contentStr, $funcFlag); }else{ $musicArray = array("title"=>$api2json[‘music‘][‘title‘], "description"=>"技术支持:LK", "MusicUrl"=>$api2json[‘music‘][‘musicurl‘], "HQMusicUrl"=>$api2json[‘music‘][‘hqmusicurl‘]); $resultStr = transmitMusic($object, $musicArray, $funcFlag,$fromUsername, $toUsername); } echo $resultStr; exit(); }
function transmitMusic($object, $musicArray, $flag = 0,$FromUserName, $toUsername) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray[‘title‘], $musicArray[‘description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $FromUserName,$toUsername, time(), $flag); echo $resultStr; }
7、手机归属地
if ($str==‘归属‘ && !empty($str_key)){ $msgType="text"; $contentStr = taobao_m($str_key); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }
function taobao_m($word){ $tmapi = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$word; $fetch = new SaeFetchurl(); $content = $fetch->fetch($tmapi); $content = iconv("gb18030","UTF-8",$content); $jso1 = "/province:\‘(.*)\‘\,/"; $jso2 = "/catName:\‘(.*)\‘\,/"; //$jso3 = "/city:\‘(.*)\‘\,/"; preg_match_all($jso1,$content,$arr1); preg_match_all($jso2,$content,$arr2); //preg_match_all($jso3,$content,$arr4); if(count($arr1) > 1){ return $arr1[1][0]."\n".$arr2[1][0];//."\n".$arr3[1][0] }else return "请重新输入电话号码!"; }
8、新闻
if($str==‘新闻‘ && !empty($str_key)){ $msgType="news"; $title="时事新闻,了解世界,了解中国"; $data = date(‘Y-m-d‘); $desription ="家事国事天下事事事关心,博古通今,坐家尽知天下事!"; $image ="http://download.hismarttv.com/epgdata/ApplicationPicture/9000001544780.jpg"; $turl ="http://news.sina.com.cn/"; $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl); echo $resultStr; }
9、成绩查询
if($str==‘成绩‘ && !empty($str_key)){ $msgType="news"; $title="广东医学院->成绩查询入口"; $data = date(‘Y-m-d‘); $desription ="保佑保佑,逢考必过,一定过啦"; $image ="http://www.chinalawedu.com/upload/html/2012/11/22/yishua9534020121122110216798374.jpg"; $turl ="http://125.90.8.125:8000/portal/home.do?method=index"; $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl); echo $resultStr; }