微信开放平台-扫码登录

admin2年前Laravel629

public function wx_info()
{
   $code = request()->input('code');
   if (!empty($code))  //有code
   {
       //通过code获得 access_token + openid
       $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid
           . "&secret=" . $this->secret . "&code=" . $code . "&grant_type=authorization_code";
       $jsonResult = file_get_contents($url);
       $resultArray = json_decode($jsonResult, true);

       $access_token = $resultArray["access_token"];
       $openid = $resultArray["openid"];
       $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;

       $infoResult = file_get_contents($infoUrl);
       $infoArray = json_decode($infoResult, true);

       $data['openid'] = $infoArray['openid'];
       $data['wx_nickname'] = $infoArray['nickname'];
       $data['avatar'] = $infoArray['headimgurl'];

       Db::beginTransaction();
       $info = Db::table('user')->where(array('id' => $this->user_id))->update($data);
       if ($info !== false) {
           Db::commit();
           return $this->success('ok', $infoArray);
       } else {
           Db::rollBack();
           return $this->error('error');
       }
   } else {
       return $this->error('请传入code值');
   }
}

相关文章

上传图片

public function image(){    $obFile = request()->file('image');    if...

百度浏览器中视频悬浮

  <video class="html5-video"                poster=...

修改密码

/** * 修改密码 */publicfunction setpass(){    $type = request()->input('type');// 1...

压缩图片

public static function size($id, $img) { try { list($width, $height, $img_type, $attr) = @getimages...

发送公众号信息

* 发送消息 */public function tosend($openid, $title, $content, $time, $aid){    $tokens = $thi...

验证手机号

public function phone($phone){    $pat = '/'        . '^13...