慕粉4345383
2017-07-08 17:05
//获取用户的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的参数
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,关闭
curl_close($ch);
if ($res == 'json') {
if ( curl_errno($ch) ) {
return curl_error($ch);
}else{
return json_decode($output,true);
}
}
}
确认APPID 和APPsecret 没错哦 把你 function getUserOpenId ()里面的 $url 的https前面的空格删掉
PHP微信公众平台开发高级篇—网页授权接口
28430 学习 · 111 问题
相似问题
回答 4
回答 3