溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在php中獲取微信openid的方法有哪些

發(fā)布時(shí)間:2021-01-27 16:41:43 來源:億速云 閱讀:169 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)在php中獲取微信openid的方法有哪些,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

<?php

/**

 * 微信授權(quán)相關(guān)接口

*/

class Wchat

{

   private $app_id = 'wx444444444444';

   private $app_secret = '77777777';

   private $state='aaaa';

  /**

   * 獲取微信授權(quán)鏈接

   * 

   * @param string $redirect_uri 跳轉(zhuǎn)地址

   * @param mixed $state 參數(shù)

   */

  public function get_authorize_url($redirect_uri = '', $state = '')

  {

    $redirect_uri = urlencode($redirect_uri);

    return "https://open.weixin.qq.com/connect/oauth3/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['code'])){

      //觸發(fā)微信返回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;

    }

     

  }

  /**

   * 獲取授權(quán)token網(wǎng)頁授權(quán)

   * 

   * @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/oauth3/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);

  }

 

}

?>

getOpenid($turl)這個(gè)方法就是獲取openid的方法。前端調(diào)用代碼如下:

$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);  

        

     }

      

   }

關(guān)于在php中獲取微信openid的方法有哪些就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI