Home >Backend Development >PHP Problem >Can php develop small programs?

Can php develop small programs?

(*-*)浩
(*-*)浩Original
2019-10-19 09:23:324178browse

Can php develop small programs?

First, take a look at the WeChat login flow chart (Recommended learning: PHP video tutorial)

Can php develop small programs? Steps: Obtain the login credentials (code) of the current logged-in WeChat user on the client. Use this credential to exchange the WeChat server for the WeChat user's unique identifier (openid) and session key (session_key). Quote WeChat encryption and decryption to obtain user information.

Note:

1. No login verification is performed.

2. For reference encryption and decryption, the path must be correct.

    //未授权用户,请求微信接口,进行授权,获取用户信息
    public function saveUser()
    {
        $appid  =  "自己的appid" ;
        $code   =   $this->input->post('code');
 
        if (empty($code)){
            return $this->fail('','code不能为空');
        }
        $encryptedData   =   $this->input->post('encryptedData');
        if (empty($encryptedData)){
            return $this->fail('','encryptedData不能为空');
        }
        $iv   =   $this->input->post('iv');
        if (empty($iv)){
            return $this->fail('','iv不能为空');
        }
 
 
        $apiData = $this->getApiData($code);
 
        if(!isset($apiData['errcode'])){
            $sessionKey = $apiData['session_key'];
            //获取sessionKey  进行解密
            $userifo = new WXBizDataCrypt($appid, $sessionKey);
            $errCode = $userifo->decryptData($encryptedData, $iv, $data );
 
            //保存
            if ($errCode == 0) {
                $data = json_decode($data,true);
                $userData = [
                    'nickname' =>$data['nickName'],
                    'headimg'  =>$data['avatarUrl'],
                    'unionid'  =>$data['unionId'],
                    'openid'  =>$data['openId'],
                    'c_time'  =>time(),
                ];
                $result = $this->AppUserModel->get(['openid'=>$data['openId']]);
 
                if ($result){
                    $this->AppUserModel->update($userData,['openid'=>$data['openId']]);
                    $returnData['uid']=$result['id'];
                    return $this->success($returnData,'已授权');
                }else{
                    $userDataId = $this->AppUserModel->add($userData);
                    if ($userDataId){
                        $returnData['uid']=$userDataId;
                        return $this->success($returnData,'已授权');
                    }else{
                        return $this->fail('','授权失败');
                    }
                }
            }
        }else{
            return $this->fail($apiData,'获取用户信息失败');
        }
    }
//获取openid
    public function getApiData($code)
    {
        $appid  =  "自己的appid" ;
        $secret =   "自己的secret";
        $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $apiData=file_get_contents($URL);
        return json_decode($apiData,true);
    }

Multiple acquisitions were not avoided. Verify before logging in.

//获取用户是否已经授权
    public function userId()
    {
 
 
        $code   =   $this->input->get('code');
        if (empty($code)){
            return $this->fail('','code不能为空');
        }
 
        $apiData = $this->getApiData($code);
        if (!isset($apiData['errcode'])){
            $openID= $apiData['openid'];
            $userData = $this->AppUserModel->get(['openid'=>$openID]);
            if (empty($userData)) {
                return $this->fail('','未授权');
            }else{
 
                //这边保存sessionKey ,方便后面手机号码授权
                $sessionKey = $apiData['session_key'];
                $mc = &load_cache('redis');
                $mc->save('session_key', $sessionKey, 3600);
                $returnData = [
                    'uid'=>$userData['id'],
                    'type'=>$userData['type']
                ];
                return $this->success($returnData,'已授权');
            }
        }else {
            return $this->fail('','获取openid失败');
        }
 
    }

The above is the detailed content of Can php develop small programs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn