php获取微信openid方法总结

yipeiwu_com6年前PHP代码库

使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的是认证服务号。

今天我要说的是第二种网页授权获取openid。下面是我写的一个关于获取openid的类

<?php

/**

 * 微信授权相关接口

 * 

 * @link http://www.phpddt.com

 */

class Wchat

{

   private $app_id = 'wx444444444444';

   private $app_secret = '77777777';

   private $state='aaaa';

  /**

   * 获取微信授权链接

   * 

   * @param string $redirect_uri 跳转地址

   * @param mixed $state 参数

   */

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

  {

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

  }

 

}

?>

getOpenid($turl)这个方法就是获取openid的方法。前端调用代码如下:

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

        

     }

      

   }

以上就是我总结的获取openid的方法啦。

以上就是php获取微信openid的详细内容,更多请关注【宜配屋www.yipeiwu.com】其它相关文章!

相关文章

通过php动态传数据到highcharts

通过php动态传数据到highcharts

1:在平时工作中,在对数据进行展示的时候,是直接通过后台提供的接口来获取json串,用来展示。今天别人问怎么在本地演示一下请求的动态数据。 2:在本地搭建环境,我用的WampServer...

php递归遍历多维数组的方法

本文实例讲述了php递归遍历多维数组的方法。分享给大家供大家参考。具体如下: <?php function get_array_elems($arrResult, $w...

PHP连接SQLSERVER 注意事项(附dll文件下载)

环境: - Apache 2.2.6 - PHP 5.2.5 - SQL Server 2005 - Windows XP SP2 步骤: 1. 首先按通常做法配置好PHP5连接MS S...

php debug 安装技巧

本打算配置ZendDebugger 进行调试,配置了老长时间没配置成功,在phpinfo看不到ZendDebugger生效,经查发现是php5.3的问题,必须先卸载,本文介绍使用xdeb...

php设计模式 Visitor 访问者模式

复制代码 代码如下:<?php /** * 访问者模式 * * 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作 * */ abs...