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

首頁 > 開發(fā) > PHP > 正文

php微信公眾平臺開發(fā)類實例

2024-05-04 21:56:01
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php微信公眾平臺開發(fā)類,實例分析了針對微信消息的響應、回復、編碼等相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php微信公眾平臺開發(fā)類。分享給大家供大家參考。具體分析如下:

ThinkWechat.php類文件如下:

  1. <?php 
  2. class Wechat { 
  3.   /** 
  4.    * 微信推送過來的數(shù)據(jù)或響應數(shù)據(jù) 
  5.    * @var array 
  6.    */ 
  7.   private $data = array(); 
  8.   /** 
  9.    * 構(gòu)造方法,用于實例化微信SDK 
  10.    * @param string $token 微信開放平臺設置的TOKEN 
  11.    */ 
  12.   public function __construct($token) { 
  13.     $this->auth($token) || exit
  14.     if(!emptyempty($_GET['echostr'])){ 
  15.       exit($_GET['echostr']); 
  16.     } else { 
  17.       try 
  18.       { 
  19.         $xml = file_get_contents("php://input"); 
  20.         $xml = new SimpleXMLElement($xml); 
  21.         $xml || exit
  22.         foreach ($xml as $key => $value) { 
  23.           $this->data[$key] = strval($value); 
  24.         } 
  25.       }catch(Exception $e){ 
  26.       } 
  27.     } 
  28.   } 
  29.   /** 
  30.    * 獲取微信推送的數(shù)據(jù) 
  31.    * @return array 轉(zhuǎn)換為數(shù)組后的數(shù)據(jù) 
  32.    */ 
  33.   public function request(){ 
  34.     return $this->data; 
  35.   } 
  36.   /** 
  37.    * * 響應微信發(fā)送的信息(自動回復) 
  38.    * @param string $to   接收用戶名 
  39.    * @param string $from  發(fā)送者用戶名 
  40.    * @param array $content 回復信息,文本信息為string類型 
  41.    * @param string $type  消息類型 
  42.    * @param string $flag  是否新標剛接受到的信息 
  43.    * @return string     XML字符串 
  44.    */ 
  45.   public function response($content$type = 'text'$flag = 0){ 
  46.     /* 基礎數(shù)據(jù) */ 
  47.     $this->data = array
  48.       'ToUserName'  => $this->data['FromUserName'], 
  49.       'FromUserName' => $this->data['ToUserName'], 
  50.       'CreateTime'  => time(), 
  51.       'MsgType'   => $type
  52.     ); 
  53.     /* 添加類型數(shù)據(jù) */ 
  54.     $this->$type($content); 
  55.     /* 添加狀態(tài) */ 
  56.     $this->data['FuncFlag'] = $flag
  57.     /* 轉(zhuǎn)換數(shù)據(jù)為XML */ 
  58.     $xml = new SimpleXMLElement('<xml></xml>'); 
  59.     $this->data2xml($xml$this->data); 
  60.     exit($xml->asXML()); 
  61.   } 
  62.   /** 
  63.    * 回復文本信息 
  64.    * @param string $content 要回復的信息 
  65.    */ 
  66.   private function text($content){ 
  67.     $this->data['Content'] = $content
  68.   } 
  69.   /** 
  70.    * 回復音樂信息 
  71.    * @param string $content 要回復的音樂 
  72.    */ 
  73.   private function music($music){ 
  74.     list( 
  75.       $music['Title'],  
  76.       $music['Description'],  
  77.       $music['MusicUrl'],  
  78.       $music['HQMusicUrl'
  79.     ) = $music
  80.     $this->data['Music'] = $music
  81.   } 
  82.   /** 
  83.    * 回復圖文信息 
  84.    * @param string $news 要回復的圖文內(nèi)容 
  85.    */ 
  86.   private function news($news){ 
  87.     $articles = array(); 
  88.     foreach ($news as $key => $value) { 
  89.       list( 
  90.         $articles[$key]['Title'], 
  91.         $articles[$key]['Description'], 
  92.         $articles[$key]['PicUrl'], 
  93.         $articles[$key]['Url'
  94.       ) = $value
  95.       if($key >= 9) { break; } //最多只允許10調(diào)新聞 
  96.     } 
  97.     $this->data['ArticleCount'] = count($articles); 
  98.     $this->data['Articles'] = $articles
  99.   } 
  100.   /** 
  101.    * 數(shù)據(jù)XML編碼 
  102.    * @param object $xml XML對象 
  103.    * @param mixed $data 數(shù)據(jù) 
  104.    * @param string $item 數(shù)字索引時的節(jié)點名稱 
  105.    * @return string 
  106.    */ 
  107.   private function data2xml($xml$data$item = 'item') { 
  108.     foreach ($data as $key => $value) { 
  109.       /* 指定默認的數(shù)字key */ 
  110.       is_numeric($key) && $key = $item
  111.       /* 添加子元素 */ 
  112.       if(is_array($value) || is_object($value)){ 
  113.         $child = $xml->addChild($key); 
  114.         $this->data2xml($child$value$item); 
  115.       } else { 
  116.         if(is_numeric($value)){ 
  117.           $child = $xml->addChild($key$value); 
  118.         } else { 
  119.           $child = $xml->addChild($key); 
  120.           $node = dom_import_simplexml($child); 
  121.           $node->appendChild($node->ownerDocument->createCDATASection($value)); 
  122.         } 
  123.       } 
  124.     } 
  125.   } 
  126.   /** 
  127.    * 對數(shù)據(jù)進行簽名認證,確保是微信發(fā)送的數(shù)據(jù) 
  128.    * @param string $token 微信開放平臺設置的TOKEN 
  129.    * @return boolean    true-簽名正確,false-簽名錯誤 
  130.    */ 
  131.   private function auth($token){ 
  132.     if(emptyempty($_GET['signature'])) return
  133.     /* 獲取數(shù)據(jù) */ 
  134.     $data = array($_GET['timestamp'], $_GET['nonce'], $token); 
  135.     $sign = $_GET['signature']; 
  136.     /* 對數(shù)據(jù)進行字典排序 */ 
  137.     sort($data,SORT_STRING); 
  138.     /* 生成簽名 */ 
  139.     $signature = sha1(implode($data)); 
  140.     return $signature === $sign
  141.   } 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 福鼎市| 五常市| 吴忠市| 两当县| 澜沧| 崇左市| 咸丰县| 平谷区| 湖北省| 建阳市| 新宾| 宁海县| 乐业县| 温泉县| 神池县| 朝阳市| 福州市| 怀宁县| 登封市| 舞阳县| 孝感市| 温泉县| 福州市| 成都市| 德化县| 蒲城县| 平阴县| 本溪市| 黔江区| 达日县| 连南| 唐山市| 石台县| 交口县| 许昌县| 长岭县| 德令哈市| 和田县| 呼玛县| 萨嘎县| 德清县|