zoukankan      html  css  js  c++  java
  • 【微信公众平台开发】封装获取天气预报功能

    版权声明:本文为博主原创文章,未经博主同意不得转载。

    https://blog.csdn.net/wyz365889/article/details/36677051


    微信公众平台小功能多点,能够添加用户的粘性,不会感觉你微信没内容,就把你给取消关注了。所以得折腾各种有的没的东西。

    个人封装天气预报功能代码例如以下(说明下:网上有非常多此类代码。可是要自己理解。就得实际折腾)
    <?

    php header('Content-Type:text/html;charset=utf-8'); class WeChatMsgType { private $toUserName; private $fromUserName; private $xmlModle; private $createTime; public function SetUserInfo($toUserName,$fromUserName) //设置用户信息 { $this->toUserName = $toUserName; $this->fromUserName = $fromUserName; $this->createTime = time(); } public function WeatherMsg($cityName) //天气预报信息 { $url = "http://api.map.baidu.com/telematics/v3/weather?location=".$cityName."&output=json&ak=11ffd27d38deda622f51c9d314d46b17"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $result = json_decode($output, true); if ($result["error"] != 0){ return $result["status"]; } $curHour = (int)date('H',time()); $weather = $result["results"][0]; $ImageTexts = array(); $ImageTexts[0] = self::SetImageTextInfo($weather['currentCity'].'天气预报',"", "", ""); //$aa=var_export(urldecode(urlencode($weather)),true); //file_put_contents("debug.txt", date("Y-m-d H:i:s",time()).$aa.PHP_EOL,FILE_APPEND); for($i=0; $i<count($weather['weather_data']); $i++) { $ImageTexts[$i+1] = self::SetImageTextInfo($weather["weather_data"][$i]["date"]." ". $weather["weather_data"][$i]["weather"]." ". $weather["weather_data"][$i]["wind"]." ". $weather["weather_data"][$i]["temperature"], "", (($curHour >= 6) && ($curHour < 18))?$weather["weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"], ""); } return self::ImageTextMsg($ImageTexts); } public function SetImageTextInfo($sTitle, $sDescription, $sPicUrl, $Url) { $item = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $ret=sprintf($item, $sTitle, $sDescription, $sPicUrl,$Url); return $ret; } public function ImageTextMsg($ImageTexts) { $this->xmlModle = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%d</ArticleCount> <Articles> "; foreach ($ImageTexts as $item) { $this->xmlModle = $this->xmlModle.$item; } $this->xmlModle = $this->xmlModle."</Articles></xml>" ; $ret=sprintf($this->xmlModle, $this->fromUserName, $this->toUserName, $this->createTime,count($ImageTexts)); return $ret; } } ?>


    调用使用方法:
    $weChatMsgType = new WeChatMsgType();
    $weChatMsgType->SetUserInfo($this->toUserName, $this->fromUserName);
    
    $resultStr=$weChatMsgType->WeatherMsg("北京");



    注:上面ak同百度周边搜索篇说明一样。不懂看前面


查看全文
  • 相关阅读:
    Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
    Hibernate逍遥游记-第9章 Hibernate的映射类型
    Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)
    Hibernate逍遥游记-第7章 Hibernate的检索策略和检索方式(<set lazy="false" fetch="join">、left join fetch、FetchMode.JOIN、)
    Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)
    Hibernate逍遥游记-第5章映射一对多-02双向(<set>、<key>、<one-to-many>、inverse、cascade="all-delete-orphan")
    Hibernate逍遥游记-第5章映射一对多-01单向<many-to-one>、cascade="save-update"、lazy、TransientObjectException
    Hibernate逍遥游记-第4章映射对象标识符-increment、identity、hilo、native、assigned、sequence、<meta>
    Hibernate逍遥游记-第3章对象-关系映射基础-access="field"、dynamic-insert、dynamic-update、formula、update=false
    CentOS 6.5安装Apache
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10662739.html
  • Copyright © 2011-2022 走看看