国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - eaglephp使用微信api接口開發(fā)微信框架

eaglephp使用微信api接口開發(fā)微信框架

2020-06-04 13:34PHP教程網(wǎng) PHP教程

EaglePHP框架開發(fā)微信5.0的API接口,包含微信5.0 API基礎(chǔ)接口、自定義菜單、高級(jí)接口,包括如下接收用戶消息、向用戶回復(fù)消息、會(huì)話界面自定義菜單、語音識(shí)別、客服接口等功能

適用平臺(tái):window/Linux
依賴項(xiàng)目:EaglePHP框架

包含微信5.0 API基礎(chǔ)接口、自定義菜單、高級(jí)接口,具體如下:
1、接收用戶消息。
2、向用戶回復(fù)消息。
3、接受事件推送。
4、會(huì)話界面自定義菜單。
5、語音識(shí)別。
6、客服接口。
7、OAuth2.0網(wǎng)頁(yè)授權(quán)。
8、生成帶參數(shù)二維碼。
9、獲取用戶地理位置。
10、獲取用戶基本信息。
11、獲取關(guān)注者列表。
12、用戶分組。

 

復(fù)制代碼 代碼如下:


<?php
/**
 * 微信公眾平臺(tái)API
 */
class WeixinChat
{

 private $token;

 private $appid;

 private $appsecret;

 private $access_token;

 // 接收的數(shù)據(jù)
 private $_receive = array();

 private $_reply = '';

 // 接口錯(cuò)誤碼
 private $errCode = '';

 // 接口錯(cuò)誤信息
 private $errMsg = '';

 // 微信oauth登陸獲取code
 const CONNECT_OAUTH_AUTHORIZE_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?';

 // 微信oauth登陸通過code換取網(wǎng)頁(yè)授權(quán)access_token
 const SNS_OAUTH_ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token?';

 // 微信oauth登陸刷新access_token(如果需要)
 const SNS_OAUTH_REFRESH_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?';

 // 通過ticket換取二維碼
 const SHOW_QRCODE_URL = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?';

 // 微信oauth登陸拉取用戶信息(需scope為 snsapi_userinfo)
 const SNS_USERINFO_URL = 'https://api.weixin.qq.com/sns/userinfo?';

 // 請(qǐng)求api前綴
 const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';

 // 自定義菜單創(chuàng)建
 const MENU_CREATE_URL = '/menu/create?';

 // 自定義菜單查詢
 const MENU_GET_URL = '/menu/get?';

 // 自定義菜單刪除
 const MENU_DELETE_URL = '/menu/delete?';

 // 獲取 access_token
 const AUTH_URL = '/token?grant_type=client_credential&';

 

 // 獲取用戶基本信息
 const USER_INFO_URL = '/user/info?';

 // 獲取關(guān)注者列表
 const USER_GET_URL = '/user/get?';

 // 查詢分組
 const GROUPS_GET_URL = '/groups/get?';

 // 創(chuàng)建分組
 const GROUPS_CREATE_URL = '/groups/create?';

 // 修改分組名
 const GROUPS_UPDATE_URL = '/groups/update?';

 // 移動(dòng)用戶分組
 const GROUPS_MEMBERS_UPDATE_URL = '/groups/members/update?';

 // 發(fā)送客服消息
 const MESSAGE_CUSTOM_SEND_URL = '/message/custom/send?';

 // 創(chuàng)建二維碼ticket
 const QRCODE_CREATE_URL = '/qrcode/create?';

 

 /**
  * 初始化配置數(shù)據(jù)
  * @param array $options
  */
 public function __construct($options)
 {
  $this->token = isset($options['token']) ? $options['token'] : '';
  $this->appid = isset($options['appid']) ? $options['appid'] : '';
  $this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : '';
 }

 
 /**
  * 獲取發(fā)來的消息
  * 當(dāng)普通微信用戶向公眾賬號(hào)發(fā)消息時(shí),微信服務(wù)器將POST消息的XML數(shù)據(jù)包到開發(fā)者填寫的URL上。
  */
 public function getRev()
 {
  $postStr = file_get_contents('php://input');
  if($postStr)
  {
   $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
   //Log::info(var_export($this->_receive, true));
  }
  return $this;
 }

 
 /**
  * 獲取微信服務(wù)器發(fā)來的消息
  */
 public function getRevData()
 {
  return $this->_receive;
 }

 
 /**
  * 獲取接收者
  */
 public function getRevTo()
 {
  return isset($this->_receive['ToUserName']) ? $this->_receive['ToUserName'] : false;
 }

 
 /**
  * 獲取消息發(fā)送者(一個(gè)OpenID)
  */
 public function getRevFrom()
 {
  return isset($this->_receive['FromUserName']) ? $this->_receive['FromUserName'] : false;
 }

 
 /**
  * 獲取接收消息創(chuàng)建時(shí)間 (整型)
  */
 public function getRevCTime()
 {
  return isset($this->_receive['CreateTime']) ? $this->_receive['CreateTime'] : false;
 }

 
 /**
  * 獲取接收消息類型(text、image、voice、video、location、link、event)
  */
 public function getRevType()
 {
  return isset($this->_receive['MsgType']) ? $this->_receive['MsgType'] : false;
 }

 
 /**
  * 獲取接收消息編號(hào)
  */
 public function getRevId()
 {
  return isset($this->_receive['MsgId']) ? $this->_receive['MsgId'] : false;
 }

 
 /**
  * 獲取接收消息文本
  * 通過語音識(shí)別接口,用戶發(fā)送的語音,將會(huì)同時(shí)給出語音識(shí)別出的文本內(nèi)容。(需申請(qǐng)服務(wù)號(hào)的高級(jí)接口權(quán)限)
  */
 public function getRevText()
 {
  if(isset($this->_receive['Content'])) return trim($this->_receive['Content']);
  elseif(isset($this->_receive['Recognition'])) return trim($this->_receive['Recognition']);
  else return false;
 }

 
 /**
  * 獲取接收?qǐng)D片消息
  */
 public function getRevImage()
 {
  if(isset($this->_receive['PicUrl'])){
   return array(
        'picUrl' => $this->_receive['PicUrl'],  //圖片鏈接
     'mediaId' => $this->_receive['MediaId'] //圖片消息媒體id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)。
       );
  }
  return false;
 }

 
 /**
  * 獲取接收語音消息
  */
 public function getRevVoice()
 {
  if(isset($this->_receive['MediaId'])){
   return array(
        'mediaId' => $this->_receive['MediaId'],  //語音消息媒體id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)。
     'format' => $this->_receive['Format'] //語音格式,如amr,speex等
       );
  }
  return false;
 }

 
 /**
  * 獲取接收視頻消息
  */
 public function getRevVideo()
 {
  if(isset($this->_receive['MediaId'])){
   return array(
        'mediaId' => $this->_receive['MediaId'],       //視頻消息媒體id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)。
     'thumbMediaId' => $this->_receive['ThumbMediaId']  //視頻消息縮略圖的媒體id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)。
       );
  }
  return false;
 } 

 
 /**
  * 獲取用戶地理位置
  */
 public function getRevLocation()
 {
  if(isset($this->_receive['Location_X'])){
   return array(
        'locationX' => $this->_receive['Location_X'],  //地理位置維度
     'locationY' => $this->_receive['Location_Y'],  //地理位置經(jīng)度
     'scale' => $this->_receive['Scale'], //地圖縮放大小
     'label' => $this->_receive['Label'] //地理位置信息
       );
  }
  //開通了上報(bào)地理位置接口的公眾號(hào),用戶在關(guān)注后進(jìn)入公眾號(hào)會(huì)話時(shí),會(huì)彈框讓用戶確認(rèn)是否允許公眾號(hào)使用其地理位置。
  //彈框只在關(guān)注后出現(xiàn)一次,用戶以后可以在公眾號(hào)詳情頁(yè)面進(jìn)行操作。
  elseif(isset($this->_receive['Latitude']))
  {
   return array(
        'latitude' => $this->_receive['Latitude'],  //地理位置緯度
     'longitude' => $this->_receive['Longitude'], //地理位置經(jīng)度
      'precision' => $this->_receive['Precision'] // 地理位置精度
       );
  }
  return false;
 }

 
 /**
  * 獲取接收鏈接消息
  */
 public function getRevLink()
 {
  if(isset($this->_receive['Title'])){
   return array(
        'title' => $this->_receive['Title'],  //消息標(biāo)題
     'description' => $this->_receive['Description'],  //消息描述
     'url' => $this->_receive['Url'] //消息鏈接
       );
  }
  return false;
 }

 
 /**
  * 獲取接收事件類型
  * 事件類型如:subscribe(訂閱)、unsubscribe(取消訂閱)、click
  */
 public function getRevEvent()
 {
  if(isset($this->_receive['Event']))
  {
   return array(
     'event' => strtolower($this->_receive['Event']),
     'key'=> isset($this->_receive['EventKey']) ? $this->_receive['EventKey'] : ''
       );
  }
  return false;
 }

 
 /**
  * 設(shè)置回復(fù)文本消息
  * @param string $content
  * @param string $openid
  */
 public function text($content='')
 {
  $textTpl = "<xml>
      <ToUserName><![CDATA[%s]]></ToUserName>
      <FromUserName><![CDATA[%s]]></FromUserName>
      <CreateTime>%s</CreateTime>
      <MsgType><![CDATA[%s]]></MsgType>
      <Content><![CDATA[%s]]></Content>
     </xml>";

  $this->_reply = sprintf($textTpl,
         $this->getRevFrom(),
         $this->getRevTo(),
         Date::getTimeStamp(),
         'text',
         $content
        );
  return $this;
 }

 
 /**
  * 設(shè)置回復(fù)音樂信息
  * @param string $title
  * @param string $desc
  * @param string $musicurl
  * @param string $hgmusicurl
  */
 public function music($title, $desc, $musicurl, $hgmusicurl='')
 {
  $textTpl = '<xml>
      <ToUserName><![CDATA[%s]]></ToUserName>
      <FromUserName><![CDATA[%s]]></FromUserName>
      <CreateTime>%s</CreateTime>
      <MsgType><![CDATA[%s]]></MsgType>
      <Music>
       <Title><![CDATA[%s]]></Title>
       <Description><![CDATA[%s]]></Description>
       <MusicUrl><![CDATA[%s]]></MusicUrl>
       <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
      </Music>
     </xml>';
  //<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>

  $this->_reply = sprintf($textTpl,
         $this->getRevFrom(),
         $this->getRevTo(),
         Date::getTimeStamp(),
         'music',
         $title,
         $desc,
         $musicurl,
         $hgmusicurl
        );
  return $this;
 }

 
 /**
  * 回復(fù)圖文消息
  * @param array
  */
 public function news($data)
 {
  $count = count($data);
  $subText = '';
  if($count > 0)
  {
   foreach($data as $v)
   {
    $tmpText = '<item>
      <Title><![CDATA[%s]]></Title>
      <Description><![CDATA[%s]]></Description>
      <PicUrl><![CDATA[%s]]></PicUrl>
      <Url><![CDATA[%s]]></Url>
      </item>';

    $subText .= sprintf(
        $tmpText, $v['title'],
        isset($v['description']) ? $v['description'] : '',
        isset($v['picUrl']) ? $v['picUrl'] : '',
        isset($v['url']) ? $v['url'] : ''
       );
   }
  }

  $textTpl = '<xml>
      <ToUserName><![CDATA[%s]]></ToUserName>
      <FromUserName><![CDATA[%s]]></FromUserName>
      <CreateTime><![CDATA[%s]]></CreateTime>
      <MsgType><![CDATA[news]]></MsgType>
      <ArticleCount><![CDATA[%d]]></ArticleCount>
      <Articles>%s</Articles>
     </xml>';

  $this->_reply = sprintf(
       $textTpl,
       $this->getRevFrom(),
       $this->getRevTo(),
       Date::getTimeStamp(),
       $count,
       $subText
      );
  return $this;
 }

 
 /**
  * 回復(fù)消息
  * @param array $msg
  * @param bool $return
  */
 public function reply()
 {
  header('Content-Type:text/xml');
  echo $this->_reply;
  exit;
 }

 
 /**
  * 自定義菜單創(chuàng)建
  * @param array 菜單數(shù)據(jù)
  */
 public function createMenu($data)
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return true;
  }

  return false;
 }

 
 /**
  * 自定義菜單查詢
  */
 public function getMenu()
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 自定義菜單刪除
  */
 public function deleteMenu()
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return true;
  }

  return false;
 }

 
 /**
  * 獲取用戶基本信息
  * @param string $openid 普通用戶的標(biāo)識(shí),對(duì)當(dāng)前公眾號(hào)唯一
  */
 public function getUserInfo($openid)
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::USER_INFO_URL.'access_token='.$this->access_token.'&openid='.$openid);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 獲取關(guān)注者列表
  * @param string $next_openid 第一個(gè)拉取的OPENID,不填默認(rèn)從頭開始拉取
  */
 public function getUserList($next_openid='')
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&next_openid='.$next_openid);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 查詢分組
  */
 public function getGroup()
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_GET_URL.'access_token='.$this->access_token);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 創(chuàng)建分組
  * @param string $name 分組名字(30個(gè)字符以內(nèi))
  */
 public function createGroup($name)
 {
  if(!$this->access_token && !$this->checkAuth()) return false;
  $data = array('group' => array('name' => $name));
  $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_CREATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return true;
  }

  return false;
 }

 
 /**
  * 修改分組名
  * @param int $id 分組id,由微信分配
  * @param string $name 分組名字(30個(gè)字符以內(nèi))
  */
 public function updateGroup($id, $name)
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $data = array('group' => array('id' => $id, 'name' => $name));
  $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_UPDATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return true;
  }

  return false;
 }

 
 /**
  * 移動(dòng)用戶分組
  *
  * @param string $openid 用戶唯一標(biāo)識(shí)符
  * @param int $to_groupid 分組id
  */
 public function updateGroupMembers($openid, $to_groupid)
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $data = array('openid' => $openid, 'to_groupid' => $to_groupid);
  $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_MEMBERS_UPDATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return true;
  }

  return false;
 }

 
 /**
  * 發(fā)送客服消息
  * 當(dāng)用戶主動(dòng)發(fā)消息給公眾號(hào)的時(shí)候(包括發(fā)送信息、點(diǎn)擊自定義菜單clike事件、訂閱事件、掃描二維碼事件、支付成功事件、用戶維權(quán)),
  * 微信將會(huì)把消息數(shù)據(jù)推送給開發(fā)者,開發(fā)者在一段時(shí)間內(nèi)(目前為24小時(shí))可以調(diào)用客服消息接口,通過POST一個(gè)JSON數(shù)據(jù)包來發(fā)送消息給普通用戶,在24小時(shí)內(nèi)不限制發(fā)送次數(shù)。
  * 此接口主要用于客服等有人工消息處理環(huán)節(jié)的功能,方便開發(fā)者為用戶提供更加優(yōu)質(zhì)的服務(wù)。
  *
  * @param string $touser 普通用戶openid
  */
 public function sendCustomMessage($touser, $data, $msgType = 'text')
 {
  $arr = array();
  $arr['touser'] = $touser;
  $arr['msgtype'] = $msgType;
  switch ($msgType)
  {
   case 'text': // 發(fā)送文本消息
    $arr['text']['content'] = $data;
    break;

   case 'image': // 發(fā)送圖片消息
    $arr['image']['media_id'] = $data;
    break;

   case 'voice': // 發(fā)送語音消息
    $arr['voice']['media_id'] = $data;
    break;

   case 'video': // 發(fā)送視頻消息
    $arr['video']['media_id'] = $data['media_id']; // 發(fā)送的視頻的媒體ID
    $arr['video']['thumb_media_id'] = $data['thumb_media_id']; // 視頻縮略圖的媒體ID
    break;

   case 'music': // 發(fā)送音樂消息
    $arr['music']['title'] = $data['title'];// 音樂標(biāo)題
    $arr['music']['description'] = $data['description'];// 音樂描述
    $arr['music']['musicurl'] = $data['musicurl'];// 音樂鏈接
    $arr['music']['hqmusicurl'] = $data['hqmusicurl'];// 高品質(zhì)音樂鏈接,wifi環(huán)境優(yōu)先使用該鏈接播放音樂
    $arr['music']['thumb_media_id'] = $data['title'];// 縮略圖的媒體ID
    break;

   case 'news': // 發(fā)送圖文消息
    $arr['news']['articles'] = $data; // title、description、url、picurl
    break;
  }

  if(!$this->access_token && !$this->checkAuth()) return false;

  $result = curlRequest(self::API_URL_PREFIX.self::MESSAGE_CUSTOM_SEND_URL.'access_token='.$this->access_token, $this->jsonEncode($arr), 'post');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return true;
  }

  return false;
 }

 

 /**
  * 獲取access_token
  */
 public function checkAuth()
 {

  // 從緩存中獲取access_token
  $cache_flag = 'weixin_access_token';
  $access_token = cache($cache_flag);
  if($access_token)
  {
   $this->access_token = $access_token;
   return true;
  }

  // 請(qǐng)求微信服務(wù)器獲取access_token
  $result = curlRequest(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$this->appid.'&secret='.$this->appsecret);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0))
   {
    $this->error($jsonArr);
   }
   else
   {
    $this->access_token = $jsonArr['access_token'];
    $expire = isset($jsonArr['expires_in']) ? intval($jsonArr['expires_in'])-100 : 3600;
    // 將access_token保存到緩存中
    cache($cache_flag, $this->access_token, $expire, Cache::FILE);
    return true;
   }
  }
  return false;
 }

 
 /**
  * 微信oauth登陸->第一步:用戶同意授權(quán),獲取code
  * 應(yīng)用授權(quán)作用域,snsapi_base (不彈出授權(quán)頁(yè)面,直接跳轉(zhuǎn),只能獲取用戶openid),
  * snsapi_userinfo (彈出授權(quán)頁(yè)面,可通過openid拿到昵稱、性別、所在地。并且,即使在未關(guān)注的情況下,只要用戶授權(quán),也能獲取其信息)
  * 直接在微信打開鏈接,可以不填此參數(shù)。做頁(yè)面302重定向時(shí)候,必須帶此參數(shù)
  *
  * @param string $redirect_uri 授權(quán)后重定向的回調(diào)鏈接地址
  * @param string $scope 應(yīng)用授權(quán)作用域 0為snsapi_base,1為snsapi_userinfo
  * @param string $state 重定向后會(huì)帶上state參數(shù),開發(fā)者可以填寫任意參數(shù)值
  */
 public function redirectGetOauthCode($redirect_uri, $scope=0, $state='')
 {
  $scope = ($scope == 0) ? 'snsapi_base' : 'snsapi_userinfo';
  $url = self::CONNECT_OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($redirect_uri).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
  redirect($url);
 }

 
 /**
  * 微信oauth登陸->第二步:通過code換取網(wǎng)頁(yè)授權(quán)access_token
  *
  * @param string $code
  */
 public function getSnsAccessToken($code)
 {
  $result = curlRequest(self::SNS_OAUTH_ACCESS_TOKEN_URL.'appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 微信oauth登陸->第三步:刷新access_token(如果需要)
  * 由于access_token擁有較短的有效期,當(dāng)access_token超時(shí)后,可以使用refresh_token進(jìn)行刷新,
  * refresh_token擁有較長(zhǎng)的有效期(7天、30天、60天、90天),當(dāng)refresh_token失效的后,需要用戶重新授權(quán)。
  *
  * @param string $refresh_token 填寫通過access_token獲取到的refresh_token參數(shù)
  */
 public function refershToken($refresh_token)
 {
  $result = curlRequest(self::SNS_OAUTH_REFRESH_TOKEN_URL.'appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 微信oauth登陸->第四步:拉取用戶信息(需scope為 snsapi_userinfo)
  * 如果網(wǎng)頁(yè)授權(quán)作用域?yàn)閟nsapi_userinfo,則此時(shí)開發(fā)者可以通過access_token和openid拉取用戶信息了。
  *
  * @param string $access_token 網(wǎng)頁(yè)授權(quán)接口調(diào)用憑證,注意:此access_token與基礎(chǔ)支持的access_token不同
  * @param string $openid 用戶的唯一標(biāo)識(shí)
  */
 public function getSnsUserInfo($access_token, $openid)
 {
  $result = curlRequest(self::SNS_USERINFO_URL.'access_token='.$access_token.'&openid='.$openid);
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 創(chuàng)建二維碼ticket
  * 每次創(chuàng)建二維碼ticket需要提供一個(gè)開發(fā)者自行設(shè)定的參數(shù)(scene_id),分別介紹臨時(shí)二維碼和永久二維碼的創(chuàng)建二維碼ticket過程。
  *
  * @param int $scene_id 場(chǎng)景值ID,臨時(shí)二維碼時(shí)為32位整型,永久二維碼時(shí)最大值為1000
  * @param int $type 二維碼類型,0為臨時(shí),1為永久
  * @param int $expire 該二維碼有效時(shí)間,以秒為單位。 最大不超過1800。
  */
 public function createQrcode($scene_id, $type=0, $expire=1800)
 {
  if(!$this->access_token && !$this->checkAuth()) return false;

  $data = array();
  $data['action_info'] = array('scene' => array('scene_id' => $scene_id));
  $data['action_name'] = ($type == 0 ? 'QR_SCENE' : 'QR_LIMIT_SCENE');
  if($type == 0) $data['expire_seconds'] = $expire;

  $result = curlRequest(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');
  if($result)
  {
   $jsonArr = json_decode($result, true);
   if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);
   else return $jsonArr;
  }

  return false;
 }

 
 /**
  * 通過ticket換取二維碼
  * 獲取二維碼ticket后,開發(fā)者可用ticket換取二維碼圖片。請(qǐng)注意,本接口無須登錄態(tài)即可調(diào)用。
  * 提醒:TICKET記得進(jìn)行UrlEncode
  * ticket正確情況下,http 返回碼是200,是一張圖片,可以直接展示或者下載。
  * 錯(cuò)誤情況下(如ticket非法)返回HTTP錯(cuò)誤碼404。
  *
  * @param string $ticket
  */
 public function getQrcodeUrl($ticket)
 {
  return self::SHOW_QRCODE_URL.'ticket='.urlencode($ticket);
 }

 
 /**
  * 記錄接口產(chǎn)生的錯(cuò)誤日志
  */
 public function error($data)
 {
  $this->errCode = $data['errcode'];
  $this->errMsg = $data['errmsg'];
  Log::info('WEIXIN API errcode:['.$this->errCode.'] errmsg:['.$this->errMsg.']');
 }

 
 /**
  * 將數(shù)組中的中文轉(zhuǎn)換成json數(shù)據(jù)
  * @param array $arr
  */
 public function jsonEncode($arr) {
     $parts = array ();
        $is_list = false;
        //Find out if the given array is a numerical array
        $keys = array_keys ( $arr );
        $max_length = count ( $arr ) - 1;
        if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
            $is_list = true;
            for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
               if ($i != $keys [$i]) { //A key fails at position check.
                  $is_list = false; //It is an associative array.
                  break;
               }
            }
        }
                foreach ( $arr as $key => $value ) {
                        if (is_array ( $value )) { //Custom handling for arrays
                                if ($is_list)
                                        $parts [] = $this->jsonEncode ( $value ); /* :RECURSION: */
                                else
                                        $parts [] = '"' . $key . '":' . $this->jsonEncode ( $value ); /* :RECURSION: */
                        } else {
                                $str = '';
                                if (! $is_list)
                                        $str = '"' . $key . '":';
                                //Custom handling for multiple data types
                                if (is_numeric ( $value ) && $value<2000000000)
                                        $str .= $value; //Numbers
                                elseif ($value === false)
                                $str .= 'false'; //The booleans
                                elseif ($value === true)
                                $str .= 'true';
                                else
                                        $str .= '"' . addslashes ( $value ) . '"'; //All other things
                                // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
                                $parts [] = $str;
                        }
                }
                $json = implode ( ',', $parts );
                if ($is_list)
                        return '[' . $json . ']'; //Return numerical JSON
                return '{' . $json . '}'; //Return associative JSON
        }

       
 /**
  * 檢驗(yàn)簽名
  */
 public function checkSignature()
 {
        $signature = HttpRequest::getGet('signature');
        $timestamp = HttpRequest::getGet('timestamp');
        $nonce = HttpRequest::getGet('nonce');

  $token = $this->token;
  $tmpArr = array($token, $timestamp, $nonce);
  sort($tmpArr);
  $tmpStr = implode($tmpArr);
  $tmpStr = sha1($tmpStr);

  return ($tmpStr == $signature ? true : false);
 }

 
 /**
  * 驗(yàn)證token是否有效
  */
 public function valid()
 {
  if($this->checkSignature()) exit(HttpRequest::getGet('echostr'));
 }

}

 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 嫩草视频在线 | 国产亚洲精品美女久久久久久久久久 | 在线观看a毛片 | 国产一级黄色大片 | av片在线观看 | 国产1级片 | 日韩国产欧美视频 | 欧美一区二区三区婷婷月色 | 一区二区三区视频免费在线观看 | 亚洲 欧美 另类 综合 偷拍 | 欧美久久久久久 | 日韩视频精品在线 | 亚洲在线| 五月天综合网 | 欧美视频一区二区 | 国产一级黄片毛片 | 欧美成人精品一区二区 | av大片| 国产一区二区在线播放 | 网站av | 一区二区在线免费观看 | 日韩理论在线 | 中文字幕高清免费日韩视频在线 | 国产自产高清不卡 | 午夜国产视频 | 欧洲毛片| 国产真实精品久久二三区 | 国内自拍视频在线观看 | 黄色片免费在线观看视频 | 国产在线不卡 | 成人av一区二区亚洲精 | 国产电影一区二区三区 | 深夜av在线 | 亚洲一区中文 | 99热视| 久久久久久亚洲 | 成人毛片在线观看视频 | t66y最新地址一地址二69 | 日韩国产精品一区二区 | 日韩免费| 91国产精品 |