php gets WeChat openid

藏色散人
Release: 2023-04-07 14:30:01
forward
3760 people have browsed it

Using the WeChat interface, whether it is automatic login or WeChat payment, the first thing we need to obtain is the openid. There are two ways to obtain the openid. One is to obtain it when paying attention. This kind of subscription number can be obtained. The second is to obtain it through web page authorization, which requires an authentication service number.

What I want to talk about today is the second kind of web page authorization to obtain openid. The following is a class I wrote about getting openid

app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
    }
     /**
     * 获取微信openid
     */
    public function getOpenid($turl)
    {
        if (!isset($_GET['code'])){
            //触发微信返回code码
            
             $url=$this->get_authorize_url($turl, $this->state);
            
            Header("Location: $url");
            exit();
        } else {
            //获取code码,以获取openid
            $code = $_GET['code'];
            $access_info = $this->get_access_token($code);
            return $access_info;
        }
        
    }
    /**
     * 获取授权token网页授权
     * 
     * @param string $code 通过get_authorize_url获取到的code
     */
    public function get_access_token($code = '')
    {
      $appid=$this->app_id;
      $appsecret=$this->app_secret;
      
        $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
        //echo $token_url;
        $token_data = $this->http($token_url);
       // var_dump( $token_data);
        if($token_data[0] == 200)
        {
            $ar=json_decode($token_data[1], TRUE);
            return $ar;
        }
        
        return $token_data[1];
    }
    
    
    public function http($url, $method='', $postfields = null, $headers = array(), $debug = false)
    {
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
 
        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);
 
        $response = curl_exec($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
 
        if ($debug) {
            echo "=====post data======\r\n";
            var_dump($postfields);
 
            echo '=====info=====' . "\r\n";
            print_r(curl_getinfo($ci));
 
            echo '=====$response=====' . "\r\n";
            print_r($response);
        }
        curl_close($ci);
        return array($http_code, $response);
    }
 
}
?>
Copy after login

getOpenid($turl) This method is the method to get openid. The front-end calling code is as follows:

      $openid=isset($_COOKIE['openid'])?$_COOKIE['openid']:'';
        
            if(empty($openid))
            {
                $wchat=new wchat();
                $t_url='http://'.$_SERVER['HTTP_HOST'].'/user.php?act=register';
                
                $info=$wchat->getOpenid($t_url);
                
                if($info){
                     $openid=$info['openid'];
                  setcookie('openid',$openid,time()+86400*30);    
                    
                }
                
            }
Copy after login

The above is the method I summarized to obtain openid.

The above is the detailed content of php gets WeChat openid. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
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 [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!