国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

thinkphp5框架結合mysql實現微信登錄和自定義分享鏈接與圖文功能示例

2024-05-05 00:10:53
字體:
來源:轉載
供稿:網友

本文實例講述了thinkphp5框架結合mysql實現微信登錄和自定義分享鏈接與圖文功能。分享給大家供大家參考,具體如下:

php代碼

function curlHtml($url){  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL, $url);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  curl_setopt($ch, CURLOPT_HEADER, 0);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  $output = curl_exec($ch);  //釋放curl句柄  curl_close($ch);  return $output;}class Wechat{  public $errmsg;  //微信登錄獲取用戶信息  public function getUserInfo() {    //1.準備scope為snsapi_base網頁授權頁面    $redirect_url = config('system.site_url') . $_SERVER["REQUEST_URI"];    $baseurl = urlencode($redirect_url);    $snsapi_base_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . config('system.appid') . '&redirect_uri=' . $baseurl . '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';    //2.靜默授權,獲取code    //頁面跳轉至redirect_uri/?code=CODE&state=STATE    $code = input('code');    if (!isset($code) || empty($code)) {      header('Location:' . $snsapi_base_url);exit(0);    }    //3.通過code換取網頁授權access_token和openID    $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . config('system.appid') . '&secret=' . config('system.appsecret') . '&code=' . $code . '&grant_type=authorization_code';    $content = curlHtml($curl);    $result = json_decode($content, true);    if(!isset($result['openid'])) {      $this->errmsg = $result['errmsg'];return false;    }    $openid = $result['openid'];    $userinfo = $this->getUserByOpenid($openid);    return $userinfo;  }  private function getUserByOpenid($openid) {    //獲取access_token    $token_info  = $this->curlGetWxAccessToken();    $access_token = $token_info['value'];    //通過OpenID來獲取用戶基本信息    $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";    $content = curlHtml($url);    $result = json_decode($content, true);    return $result;  }  /**   * [wxShare 微信分享]   * @param [type] $url [description]   * @return [type]   [description]   */  public function wxShare()  {    $noncestr = uniqid();    $timestamp = time();    $url = config('system.site_url') . $_SERVER["REQUEST_URI"];    // $redis   = new /Redis;    // $ticket_key = 'wx_ticket';    // $ticket   = $redis->get($ticket_key);    // if (!$ticket) {    //   $ticket = $this->getJsapiTicket();    //   $redis->set($ticket_key, $ticket);    //   $redis->expire($ticket_key, 7200);    // }    $ticket = $this->getJsapiTicket();    if ($ticket) {      $str     = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '×tamp=' . $timestamp . '&url=' . $url;      $signature  = sha1($str);      $return_data = [        'noncestr' => $noncestr,        'timestamp' => $timestamp,        'signature' => $signature,        'appid'   => config('system.appid'),        'link'   => $url,      ];      return $return_data;    }  }  private function getJsapiTicket()  {    $map['keyname'] = 'Ticket';    $map['modifytime'] = array('GT', time() - 7200);    $return       = WxTokenModel::getOne('*', $map);    if ($return) {      return $return['value'];    } else {      $token_info  = $this->curlGetWxAccessToken();      $access_token = $token_info['value'];      $url     = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi';      $output    = curlHtml($url);      $data     = json_decode($output, true);      if (isset($data['errcode']) && $data['errcode'] == 0) {        $condition['keyname'] = 'Ticket';        $update_data['modifytime'] = time();        $update_data['value']   = $data['ticket'];        $up_result      = WxTokenModel::updateData($condition, $update_data);        if ($up_result !== false) {          return $data['ticket'];        }      }    }    return false;  }  private function curlGetWxAccessToken()  {    $map['keyname'] = 'AccessToken';    $map['modifytime'] = array('GT', time() - 7200);    $return = WxTokenModel::getOne('*', $map);    if ($return) {      return $return;    } else {      $url  = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . config('system.appid') . '&secret=' . config('system.appsecret');      $output = curlHtml($url);      $data = json_decode($output, true);      if ($data && isset($data['access_token'])) {        $condition['keyname'] = 'AccessToken';        $update_data['modifytime'] = time();        $update_data['value'] = $data['access_token'];        $up_result      = WxTokenModel::updateData($condition, $update_data);        if ($up_result !== false) {          return $update_data;        }      }    }    return false;  }}

html代碼

<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script><script>wx.config({   debug: false,   appId: '{$appid}', // 必填,公眾號的唯一標識   timestamp: '{$timestamp}', // 必填,生成簽名的時間戳   nonceStr: '{$noncestr}', // 必填,生成簽名的隨機串   signature: '{$signature}',// 必填,簽名,見附錄1   jsApiList: ['onMenuShareTimeline'] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2});wx.ready(function () {  // 分享到朋友圈  wx.onMenuShareTimeline({   title: '', // 名   link: '{$link}', // 地址   imgUrl: '', // 分享的圖標   success: function () {   // 用戶確認分享后執行的回調函數   },   cancel: function () {    // 用戶取消分享后執行的回調函數  }  });});</script>

希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 博爱县| 千阳县| 织金县| 青阳县| 凭祥市| 临清市| 扎鲁特旗| 体育| 伊宁市| 三亚市| 峡江县| 汨罗市| 南阳市| 曲靖市| 中江县| 望城县| 柳河县| 安平县| 阿拉尔市| 扎兰屯市| 麻江县| 巨野县| 津市市| 衡水市| 宁都县| 务川| 鸡泽县| 濉溪县| 华安县| 金阳县| 兴义市| 漳平市| 区。| 墨江| 桑植县| 苗栗市| 友谊县| 余庆县| 腾冲县| 隆安县| 德兴市|