百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> php猎取微信openid
分享文章到:

php猎取微信openid

发布时间:09/01 来源:未知 浏览: 关键词:
使用微信接口,不管是主动登录还是微信支付我们第一需要猎取的就是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的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板