var_dump($res);返回NULL是什么原因?
//獲取用戶的openid
? ? public function getBaseInfo(){
? ? ? ? //1.獲取到code
? ? ? ? $appid = "wxd3fa5caeaf0a1844";
? ? ? ? $redirect_uri=urlencode("http://wx.hodila.com/Index/getUserOpenId");
? ? ? ? $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
? ? ? ? header('location:'.$url);
? ? }
? ? public function getUserOpenId(){
? ? ? ? $appid="wxd3fa5caeaf0a1844";
? ? ? ? $appsecret="34cfc7d2f06a3bfdf3165f93d09aa3b0";
? ? ? ? $code=$_GET['code'];
? ? ? ? $url=" https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
? ? ? ? //拉取用戶的openid
? ? ? ? $res=$this->http_curl($url,'get');
? ? ? ? var_dump($res);?
? ? }
? ? public function http_curl($url,$type='get',$res='json',$arr=''){
? ? ? ? //1,初始化curl
? ? ? ? $ch = curl_init();
? ? ? ? //2,設置 curl的參數(shù)
? ? ? ? curl_setopt($ch, CURLOPT_URL, $url);
? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
? ? ? ? if ($type == 'post') {
? ? ? ? ? ? curl_setopt($ch, CURLOPT_POST, $url);
? ? ? ? ? ? curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
? ? ? ? }
? ? ? ? //3,采集
? ? ? ? $output = curl_exec($ch);
? ? ? ? //4,關(guān)閉
? ? ? ? curl_close($ch);
? ? ? ? if ($res == 'json') {
? ? ? ? ? ? if ( curl_errno($ch) ) {
? ? ? ? ? ? ? ? return curl_error($ch);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return json_decode($output,true);
? ? ? ? ? ? }
? ? ? ? }
? ? }
2017-07-12
確認APPID 和APPsecret 沒錯哦 把你?function getUserOpenId ()里面的 $url 的https前面的空格刪掉?