日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

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

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:PHP生成指定隨機字符串的簡單實現方法
具體分析如下: 這是一個簡單的函數,沒有對生成的內容作強制設定。所以在生成的字符串長度較少的時候,會出現沒有指定類型字符的情況。當然,修改起來也很簡單,這里就不做添加了。

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

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

ThinkWechat.php類文件如下:

  1. <?php 
  2. class Wechat { 
  3.   /** 
  4.    * 微信推送過來的數據或響應數據 
  5.    * @var array 
  6.    */ 
  7.   private $data = array(); 
  8.   /** 
  9.    * 構造方法,用于實例化微信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.    * 獲取微信推送的數據 
  31.    * @return array 轉換為數組后的數據 
  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.     /* 基礎數據 */ 
  47.     $this->data = array
  48.       'ToUserName'  => $this->data['FromUserName'], 
  49.       'FromUserName' => $this->data['ToUserName'], 
  50.       'CreateTime'  => time(), 
  51.       'MsgType'   => $type
  52.     ); 
  53.     /* 添加類型數據 */ 
  54.     $this->$type($content); 
  55.     /* 添加狀態(tài) */ 
  56.     $this->data['FuncFlag'] = $flag
  57.     /* 轉換數據為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 要回復的圖文內容 
  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調新聞 
  96.     } 
  97.     $this->data['ArticleCount'] = count($articles); 
  98.     $this->data['Articles'] = $articles
  99.   } 
  100.   /** 
  101.    * 數據XML編碼 
  102.    * @param object $xml XML對象 
  103.    * @param mixed $data 數據 
  104.    * @param string $item 數字索引時的節(jié)點名稱 
  105.    * @return string 
  106.    */ 
  107.   private function data2xml($xml$data$item = 'item') { 
  108.     foreach ($data as $key => $value) { 
  109.       /* 指定默認的數字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.    * 對數據進行簽名認證,確保是微信發(fā)送的數據 
  128.    * @param string $token 微信開放平臺設置的TOKEN 
  129.    * @return boolean    true-簽名正確,false-簽名錯誤 
  130.    */ 
  131.   private function auth($token){ 
  132.     if(emptyempty($_GET['signature'])) return
  133.     /* 獲取數據 */ 
  134.     $data = array($_GET['timestamp'], $_GET['nonce'], $token); 
  135.     $sign = $_GET['signature']; 
  136.     /* 對數據進行字典排序 */ 
  137.     sort($data,SORT_STRING); 
  138.     /* 生成簽名 */ 
  139.     $signature = sha1(implode($data)); 
  140.     return $signature === $sign
  141.   } 

分享:php使用Image Magick將PDF文件轉換為JPG文件的方法
這是一個非常簡單的格式轉換代碼,可以把.PDF文件轉換為.JPG文件,代碼要起作用,服務器必須要安裝Image Magick 擴展。

來源:模板無憂//所屬分類:PHP教程/更新時間:2015-04-02
相關PHP教程