求大神指教.为什么什么效果都没有了,数据部分代码放不下了

来源:2-2 自定义菜单代码实现

qq_T_T若是人间四月天_0

2017-04-17 00:07

public function responseMsg(){


    $postArr = file_get_contents('php://input');
    $postObj = simplexml_load_string($postArr);//将xml数据转为对象
    //判断该数据包是否是订阅的事件推送
    if( strtolower( $postObj -> MsgType) == 'event'){
            //如果是关注公众号事件即subscribe则回复消息
             if(strtolower( $postObj -> Event) == 'subscribe'){
                  $arr =array(
                            array(
                                'title'=>"您已成功关注该平台",
                                'description'=>"百度一下,你就知道",
                                'url'=>"http://www.baidu.com",
                                'picUrl'=>"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"
                            )

                         );//图文信息,可从数据库获取
                  $indexModel = new IndexModel();
                  $indexModel -> responseSubscribe($postObj,$arr);
             }
        }
        //自定义菜单事件
         if( strtolower($postObj->Event) == 'click'){
             if( strtolower($postObj->EventKey) == 'tuwen'){
             
           //引入自定义模型类文件,sdk
              $indexModel = new IndexModel();
              $indexModel -> responseNews($postObj,$arr);
            }
            if( strtolower($postObj->EventKey) == 'WEATHER'){
                     $city = urlencode("呼和浩特");
                    $res = $this -> http_weather($city);
                    $arr =json_decode($res,true);
                    $content = "当前实况天气:"."\n"."当前温度:"."\n".$arr["result"]["sk"]["temp"]."\n"."当前风向:"."\n".$arr["result"]["sk"]["wind_direction"]."\n"."当前风力:"."\n".$arr["result"]["sk"]["wind_strength"]."\n"."更新时间:"."\n".$arr["result"]["sk"]["time"];;
                         //实例化sdk模型
                     $indexModel = new IndexModel();
                     $indexModel -> responseText($postObj,$content);
            }
         }
             
        if( strtolower($postObj->Event) == 'view'){
             $content = "跳转";
                         //实例化sdk模型
                     $indexModel = new IndexModel();
                     $indexModel -> responseText($postObj,$content);
        }
    }
    /*
        $url 请求地址
        $type 请求方式
        $res 返回数据的类型
        $arr post传递的参数
    */
     public function http_curl($url,$type='get',$res='json',$arr=''){
          //通过php的curl工具采集某个url的内容
        //1.初始化curl
        $ch = curl_init();
        //2.设置请求选项, 包括具体的url,默认为get请求方式
        curl_setopt($ch,CURLOPT_URL,$url);//需要获取的URL地址,也可以在curl_init()函数中设置
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
        //3.采集内容
         if($type == 'post'){
              curl_setopt($ch,CURLOPT_POST,1);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
             curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
         }
        $response = curl_exec($ch);//执行一个cURL会话
        //4.关闭CURL会话
        curl_close($ch);
           if($res=='json'){
             if (curl_errno($url)) {
                  echo "cURL Error #:" . curl_error($url);
                    } else {
                  return json_decode($response,true);
                    }
        }
    }
    public function http_weather($city){
       //根据第三方api实现天气查询功能
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "http://apis.haoservice.com/weather?cityname=".$city."&key=229e117ce73e40bcb9f8451b93abb285",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          return $response;
        }
    }
  
    public function getWXAccessToken(){
        //从session/cookie中获取accessToken或通过curl获取acceess_token并存入session
        if($_SESSION['acceess_token'] && $_SESSION['expire_time']>time()){
            //session中有acceess_token并且未过期
            return $_SESSION['acceess_token'];
        }else{
            $appid="wxa22f6d2e559d8dcb";
            $appsecret="35e42cd5420e4c90fc2dc6e53cb04ec8";
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
            $res = $this -> http_curl($url,'get','json');
            $access_token = $res['access_token '];
            $_SESSION['acceess_token'] = $access_token;
            $_SESSION['expire_time'] = time() + 7200;
            return  $access_token;
        }
    }
    public function defineMenu(){
         echo "ss";
    //自定义菜单
    //微信接口通过curl post/get链接
     header("Content-type: text/html; charset=utf-8");
     $access_token = $this -> getWXAccessToken();
     $url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token."";
     $postArr = array(
             "button" => array(
                         "type" => "click",
                       "name":urlencode("今时天气"),
                       "key":"WEATHER"
            ),//一级菜单一
             array(
             "name" =>urlencode("更多"),
               "sub_button" => array(
               array(    
                   "type" => "view",
                   "name" => urlencode("我的微店"),
                   "url" => "https://weidian.com/?userid=1203450123&wfr=c&ifr=shopdetail"
                ),
                array(
                     "type" => "miniprogram",
                     "name" => "wxa",
                     "url" => "http://mp.weixin.qq.com",
                     "appid" => "wx286b93c14bbf93aa",
                     "pagepath" => "pages/lunar/index.html"
                 ),
                 array(
                   "type" => "click",
                   "name" => urlencode("赞一下我们"),
                   "key" => "tuwen"
                )
             )
            )
     );
      $postJSON = urldecode(json_encode($postArr)) ;
     $res = $this -> http_curl($url,'post','json', $postArr);
    }

写回答 关注

1回答

  • 鱼儿鱼儿鱼儿鱼儿
    2017-06-12 12:46:55
    已采纳

    最后面,$res=this->http_curl()里面的形参写错了,最后一个参数不是$postArr,而是$postJSON

PHP微信公众平台开发高级篇—自定义菜单

自定义菜单是微信开发中必不可少的环节,约吗?

28920 学习 · 81 问题

查看课程

相似问题