php微信 - 7curl get 和post封裝

封裝curl

 public function http_curl($url, $type = 'get', $res = 'json', $arr = ''){
        $cl = curl_init();
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        if($type == 'post'){
            curl_setopt($cl, CURLOPT_POST, 1);
            curl_setopt($cl, CURLOPT_POSTFIELDS, $arr);
        }
        $output = curl_exec($cl);
        curl_close($cl);
        if($res == 'json'){
            if( curl_error($cl)){
                return curl_error($cl);
            }else{
                return json_decode($output, true);
            }
        }
    }

post

 // 創建微信自定義菜單
    public function WxMenu(){
        $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' => 'video',
                ),
                //第二個一級菜單
                array(
                    'name' => urlencode('菜單二'),
                    'sub_button' => array(
                        array(
                            'name' => urlencode('歌曲'),
                            'type' => 'click',
                            'key' => 'songs',   
                        ),
                        array(
                            'name' => urlencode('電影'),
                            'type' => 'view',
                            'url' => 'http://www.baidu.com',
                        ),
                    ),
                ),
                //第三個一級菜單
                array(
                    'name' => urlencode('菜單三'),
                    'type' => 'view',
                    'url' => 'http://www.qq.com',
                ),
            ),
        );
        $postJson = urldecode( json_encode($postArr) );
        //post
        $res = $this->http_curl($url, 'post', 'json', $postJson);
    }

get

    // 獲取微信服務器IP地址
    public function getWxServerIp(){
        $access_token = $this->getWxAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token='.$access_token;
        //get
        $res = $this->http_curl($url,'get','json');
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章