首页 > 代码库 > PHP之路——微信公众号授权获取用户信息

PHP之路——微信公众号授权获取用户信息

官方文档链接:http://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html

 

  /**     * 获取code     */    public function actionGetCode()    {        $response_type = ‘code‘;        $scope = ‘snsapi_userinfo‘;        $conf = yii::$app->params[‘oauth_conf‘][‘oauth_wx_in‘];        $url = ‘https://open.weixin.qq.com/connect/oauth2/authorize?‘;        $url .= ‘appid=‘.$conf[‘app_id‘];        $url .= ‘&redirect_uri=‘ . urlencode($conf[‘redirect_uri‘]);        $url .= ‘&response_type=code‘;    //这是微信公众号内获取        $url .= ‘&scope=snsapi_userinfo‘;    //这是微信开放平台获取//        $url .= ‘&scope=snsapi_login‘;        $url .= ‘&#wechat_redirect‘;        header(‘Location:‘.$url);            }    /**     * 获取access_token     */    public function actionGettoken()    {        $conf = yii::$app->params[‘oauth_conf‘][‘oauth_wx_in‘];        $code = $_GET[‘code‘];        $url = ‘https://api.weixin.qq.com/sns/oauth2/access_token?‘;        $url .= ‘appid=‘.$conf[‘app_id‘];        $url .= ‘&secret=‘.$conf[‘app_key‘];        $url .= ‘&code=‘.$code;        $url .= ‘&grant_type=authorization_code‘;        $ch = curl_init();        curl_setopt($ch,CURLOPT_URL,$url);        curl_setopt($ch,CURLOPT_HEADER,0);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);        $data = http://www.mamicode.com/curl_exec($ch);>

 

PHP之路——微信公众号授权获取用户信息