【微信公众平台】调用接口,实现天气预报代码。

<?php


define("TOKEN", "xujing");  


$wechatObj = new wechatCallbackapiTest();  //创建对象;
if (!isset($_GET['echostr'])) {  //iset用于检测变量是否赋值。
    $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);    //sha1() 函数计算字符串的 SHA-1 散列,成功返回序列,否则返回false
        if($tmpStr == $signature){ 
            echo $echoStr;       //如果验证身份成功,则返回值。
            exit;
        }
    }


    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];// 接收data格式。
        if (!empty($postStr)){                 
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);// 把 XML 字符串载入对象中。如果失败,则返回 false。把$postStr转成简单的'SimpleXMLElement'。
            $RX_TYPE = trim($postObj->MsgType);//trim() 函数从字符串的两端删除空白字符和其他预定义字符。


            $result = "";
            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) 
    {
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注后会有柒,回复任意城市名,如:北京。即可获得该城市的近期天气预报";
                break;
        }
        $result = $this->transmitText($object, $content);
        return $result;
    }


    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        $url = "http://apix.sinaapp.com/weather/?appkey=".$object->ToUserName."&city=".urlencode($keyword); //此处是在调用接口。该接口不用自己找到源码编辑,直接用即可。
        $output = file_get_contents($url);//把整个文件读入一个字符串中于.将文件的内容读入到一个字符串中的首选方法
        $content = json_decode($output, true);
                  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
        $result = $this->transmitNews($object, $content);
        return $result;
    }


    private function transmitText($object, $content)
    {
        if (!isset($content) || empty($content)){
            return "";
        }
        $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); //把数据插入textTP1中的%处。
        return $result;
    }


    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return "";
        }
        $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
        $item_str = "";
        foreach ($newsArray 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($newsArray));
        return $result;
    }


    private function logger($log_content)
    {
      
    }
}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值