Maison  >  Article  >  développement back-end  >  php obtient l'openid de WeChat

php obtient l'openid de WeChat

藏色散人
藏色散人avant
2019-10-10 14:24:413862parcourir

En utilisant l'interface WeChat, qu'il s'agisse d'une connexion automatique ou d'un paiement WeChat, la première chose que nous devons obtenir est l'openid. Il existe deux façons d'obtenir l'openid. La première est de l'obtenir en faisant attention à ce type d'abonnement. Le numéro peut être obtenu. La seconde consiste à l'obtenir via une autorisation de page Web, qui nécessite un numéro de service d'authentification.

Ce dont je vais parler aujourd'hui, c'est du deuxième type d'autorisation de page Web pour obtenir openid. Ce qui suit est un cours que j'ai écrit sur l'obtention d'openid.

<?php
/**
 * 微信授权相关接口
 * 
 * @link http://www.phpddt.com
 */
class Wchat
{
      private $app_id = &#39;wx444444444444&#39;;
      private $app_secret = &#39;77777777&#39;;
     private $state=&#39;aaaa&#39;;
   /**
     * 获取微信授权链接
     * 
     * @param string $redirect_uri 跳转地址
     * @param mixed $state 参数
     */
    public function get_authorize_url($redirect_uri = &#39;&#39;, $state = &#39;&#39;)
    {
        $redirect_uri = urlencode($redirect_uri);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
    }
     /**
     * 获取微信openid
     */
    public function getOpenid($turl)
    {
        if (!isset($_GET[&#39;code&#39;])){
            //触发微信返回code码
            
             $url=$this->get_authorize_url($turl, $this->state);
            
            Header("Location: $url");
            exit();
        } else {
            //获取code码,以获取openid
            $code = $_GET[&#39;code&#39;];
            $access_info = $this->get_access_token($code);
            return $access_info;
        }
        
    }
    /**
     * 获取授权token网页授权
     * 
     * @param string $code 通过get_authorize_url获取到的code
     */
    public function get_access_token($code = &#39;&#39;)
    {
      $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=&#39;&#39;, $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 &#39;POST&#39;:
                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 &#39;=====info=====&#39; . "\r\n";
            print_r(curl_getinfo($ci));
 
            echo &#39;=====$response=====&#39; . "\r\n";
            print_r($response);
        }
        curl_close($ci);
        return array($http_code, $response);
    }
 
}
?>

getOpenid($turl) Cette méthode est la méthode pour obtenir openid. Le code d'appel frontal est le suivant :

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

Ce qui précède est la méthode que j'ai résumée pour obtenir l'openid.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer