微信公众号开发入门---STATE 2:获取用户信息

微信以openid获取用户信息
//先获取access_token
$wxin_conf = Yii::$app->params[‘oauth_conf‘][‘oauth_wx_in‘];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$wxin_conf[‘app_id‘]."&secret=".$wxin_conf[‘app_key‘];
$data = CurlslUtil::get($url);
$data = json_decode($data);
$token = @$data->access_token;
//获取userinfo
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=olKhow5AWI40PPzeeMVAjSXRFb3c&lang=zh_CN]";
$data = CurlslUtil::get($url);
$data = @json_decode($data);
var_dump($data);
 
//curl封装
public static function get($url, $asyncPost = 0, $header = array()) {
if ($asyncPost == 1)
$header [] = "Connection: close";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POST, false );
if (substr ( $url, 0, 5 ) == ‘https‘) {
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
}
$data = curl_exec ( $ch );
curl_close ( $ch );
return $data;
}

微信公众号开发入门---STATE 2:获取用户信息

上一篇:微信公众号开发入门---STATE 3:模板推送


下一篇:微信公众号开发入门---STATE 1:入门回复,菜单