php微信

来源:互联网 发布:xampp mysql mac设置 编辑:程序博客网 时间:2024/05/24 15:37

封装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');    }
原创粉丝点击