PHP를 사용하여 WeChat 웹 페이지 인증 개발을 구현하는 단계

高洛峰
풀어 주다: 2017-03-12 14:53:15
원래의
2016명이 탐색했습니다.

이 글은 주로 PHP 구현 WeChat 웹페이지 인증 개발 튜토리얼을 공유합니다. 개발자는 인증 후 사용자의 기본 정보를 얻을 수 있습니다.

WeChat 웹페이지 인증은 서비스 계정에서만 사용할 수 있는 고급 기능. 개발자는 인증을 통해 사용자의 기본 정보를 얻을 수 있습니다. 그 전에는 사용자가 WeChat 웹과 상호 작용할 때 openid를 기반으로 사용자 정보를 얻을 수 있습니다. 페이지 인증은 메시지 상호작용이나 주의가 필요 없이 사용자의 기본 정보를 얻을 수 있습니다.

PHP를 사용하여 WeChat 웹 페이지 인증 개발을 구현하는 단계

WeChat 웹페이지 인증은 OAuth2.0을 통해 완료됩니다.

  • 사용자가 코드를 승인하고 얻습니다.

  • 코드에 따라 access_token을 얻습니다. [refresh_token을 통해 더 긴 유효 기간을 얻을 수 있습니다.]

  • access_token 및 openid를 통해 사용자 정보 가져오기

WeChat 웹페이지 승인 과정은 간단하게 요약되어 있습니다:


 <?php
 
/**
 * 微信授权相关接口
 */
 
class Wechat {
  
  //高级功能-》开发者模式-》获取
  private $app_id = &#39;xxx&#39;;
  private $app_secret = &#39;xxxxxxx&#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";
  }
  
  /**
   * 获取授权token
   * 
   * @param string $code 通过get_authorize_url获取到的code
   */
  public function get_access_token($app_id = &#39;&#39;, $app_secret = &#39;&#39;, $code = &#39;&#39;)
  {
    $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";
    $token_data = $this->http($token_url);
    
    if($token_data[0] == 200)
    {
      return json_decode($token_data[1], TRUE);
    }
    
    return FALSE;
  }
  
  /**
   * 获取授权后的微信用户信息
   * 
   * @param string $access_token
   * @param string $open_id
   */
  public function get_user_info($access_token = &#39;&#39;, $open_id = &#39;&#39;)
  {
    if($access_token && $open_id)
    {
      $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
      $info_data = $this->http($info_url);
      
      if($info_data[0] == 200)
      {
        return json_decode($info_data[1], TRUE);
      }
    }
    
    return FALSE;
  }
  
  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 &#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);
  }
 
}
로그인 후 복사

위 내용은 이 글의 전체 내용입니다. 모든 분들의 학습에 도움이 되기를 바랍니다.

위 내용은 PHP를 사용하여 WeChat 웹 페이지 인증 개발을 구현하는 단계의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!