function http_curl($url,$type='get',$res='json',$arr=''){ // 1.初始化curl $ch = curl_init(); // $url = "https://www.imooc.com/"; // 2.设置curl参数 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if($type=='post'){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); } // 3.采集 $output = curl_exec($ch); // 4.关闭 curl_close($ch); if($res=="json"){ if(curl_error($ch)){ return curl_error($ch); }else{ return json_decode($output,true); } } } public function getWxAccessToken(){ if($_SESSION['access_token'] && $_SESSION['expire_time']>time()){ return $_SESSION['access_token']; }else{ $appid = 'wx87597d10a4f61fc3'; $appsecret = "bfcde589d19fd16c61d1142633f6872b"; $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['access_token'] = $access_token; $_SESSION['expire_time'] =time()+7000; return $access_token; var_dump($access_token); } } public function definedItem(){ $access_token = $this->getWxAccessToken(); $url =" https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; $postArr = array( 'button'=>array( array( 'name'=>urlencode('菜单一'), 'type'=>'click', 'key'=>'item1' ),//第一个一级菜单 array( 'name'=>urlencode('菜单二'), 'sub_button'=>array( array( 'name'=>urlencode('歌曲'), 'type'=>'click', 'key'=>'songs' ), array( 'name'=>urlencode('百度'), 'type'=>'view', 'url'=>'http://wap.baidu.com/' ), ), ),//第二个一级菜单 array( 'type'=>'click', 'name'=>'赞一下我们', 'key'=>'V1001_GOOD' ),//第三个一级菜单 ), ); $postJosn =urldecode(json_encode($postArr)); $res = $this->http_curl($url,'post','josn',$postJosn); var_dump($res); }
小苑Coder
12345654321
慕田峪3555374