求大神指教.為什么什么效果都沒(méi)有了,數(shù)據(jù)部分代碼放不下了
public function responseMsg(){
??? $postArr = file_get_contents('php://input');
??? $postObj = simplexml_load_string($postArr);//將xml數(shù)據(jù)轉(zhuǎn)為對(duì)象
??? //判斷該數(shù)據(jù)包是否是訂閱的事件推送
??? if( strtolower( $postObj -> MsgType) == 'event'){
?????? ??? ?//如果是關(guān)注公眾號(hào)事件即subscribe則回復(fù)消息
?????? ??? ? if(strtolower( $postObj -> Event) == 'subscribe'){
??????????? ??? ? $arr =array(
??????????????????????????? array(
??????????????????????????????? 'title'=>"您已成功關(guān)注該平臺(tái)",
??????????????????????????????? 'description'=>"百度一下,你就知道",
??????????????????????????????? 'url'=>"http://www.baidu.com",
??????????????????????????????? 'picUrl'=>"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"
??????????????????????????? )
???????????????????????? );//圖文信息,可從數(shù)據(jù)庫(kù)獲取
????????????????? $indexModel = new IndexModel();
????????????????? $indexModel -> responseSubscribe($postObj,$arr);
???????????? }
?? ??? ?}
?? ??? ?//自定義菜單事件
?? ??? ? if( strtolower($postObj->Event) == 'click'){
??????? ??? ?if( strtolower($postObj->EventKey) == 'tuwen'){
?????????? ??
?????????? //引入自定義模型類(lèi)文件,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 = "當(dāng)前實(shí)況天氣:"."\n"."當(dāng)前溫度:"."\n".$arr["result"]["sk"]["temp"]."\n"."當(dāng)前風(fēng)向:"."\n".$arr["result"]["sk"]["wind_direction"]."\n"."當(dāng)前風(fēng)力:"."\n".$arr["result"]["sk"]["wind_strength"]."\n"."更新時(shí)間:"."\n".$arr["result"]["sk"]["time"];;
????????? ??? ??? ??? ?? //實(shí)例化sdk模型
??????????? ??? ??? ?$indexModel = new IndexModel();
??????????? ??? ??? ?$indexModel -> responseText($postObj,$content);
??????????? }
???????? }
??????????? ?
??????? if( strtolower($postObj->Event) == 'view'){
???????????? $content = "跳轉(zhuǎn)";
????????? ??? ??? ??? ?? //實(shí)例化sdk模型
??????????? ??? ??? ?$indexModel = new IndexModel();
??????????? ??? ??? ?$indexModel -> responseText($postObj,$content);
??????? }
??? }
?? ?/*
?? ??? ?$url 請(qǐng)求地址
??????? $type 請(qǐng)求方式
??????? $res 返回?cái)?shù)據(jù)的類(lèi)型
??????? $arr post傳遞的參數(shù)
??? */
???? public function http_curl($url,$type='get',$res='json',$arr=''){
?? ??? ?? //通過(guò)php的curl工具采集某個(gè)url的內(nèi)容
??????? //1.初始化curl
??????? $ch = curl_init();
??????? //2.設(shè)置請(qǐng)求選項(xiàng), 包括具體的url,默認(rèn)為get請(qǐng)求方式
??????? curl_setopt($ch,CURLOPT_URL,$url);//需要獲取的URL地址,也可以在curl_init()函數(shù)中設(shè)置
??????? curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//將curl_exec()獲取的信息以文件流的形式返回,而不是直接輸出。
??????? //3.采集內(nèi)容
???????? if($type == 'post'){
??????? ??? ? curl_setopt($ch,CURLOPT_POST,1);//啟用時(shí)會(huì)發(fā)送一個(gè)常規(guī)的POST請(qǐng)求,類(lèi)型為:application/x-www-form-urlencoded,就像表單提交的一樣。
???????????? curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
???????? }
??????? $response = curl_exec($ch);//執(zhí)行一個(gè)cURL會(huì)話
??????? //4.關(guān)閉CURL會(huì)話
??????? 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){
?????? //根據(jù)第三方api實(shí)現(xiàn)天氣查詢功能
?? ??? ?$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或通過(guò)curl獲取acceess_token并存入session
??????? if($_SESSION['acceess_token'] && $_SESSION['expire_time']>time()){
?????? ??? ?//session中有acceess_token并且未過(guò)期
??????????? 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";
??? //自定義菜單
??? //微信接口通過(guò)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("今時(shí)天氣"),
???????? ??? ??? ??? ? "key":"WEATHER"
??????????? ),//一級(jí)菜單一
??????? ??? ?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);
??? }
2017-06-12
最后面,$res=this->http_curl()里面的形參寫(xiě)錯(cuò)了,最后一個(gè)參數(shù)不是$postArr,而是$postJSON