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

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

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

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

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

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

ThinkWechat.php類文件如下:

  1. <?php 
  2. class Wechat { 
  3.   /** 
  4.    * 微信推送過來的數(shù)據(jù)或響應(yīng)數(shù)據(jù) 
  5.    * @var array 
  6.    */ 
  7.   private $data = array(); 
  8.   /** 
  9.    * 構(gòu)造方法,用于實(shí)例化微信SDK 
  10.    * @param string $token 微信開放平臺設(shè)置的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.    * * 響應(yīng)微信發(fā)送的信息(自動回復(fù)) 
  38.    * @param string $to   接收用戶名 
  39.    * @param string $from  發(fā)送者用戶名 
  40.    * @param array $content 回復(fù)信息,文本信息為string類型 
  41.    * @param string $type  消息類型 
  42.    * @param string $flag  是否新標(biāo)剛接受到的信息 
  43.    * @return string     XML字符串 
  44.    */ 
  45.   public function response($content$type = 'text'$flag = 0){ 
  46.     /* 基礎(chǔ)數(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.    * 回復(fù)文本信息 
  64.    * @param string $content 要回復(fù)的信息 
  65.    */ 
  66.   private function text($content){ 
  67.     $this->data['Content'] = $content
  68.   } 
  69.   /** 
  70.    * 回復(fù)音樂信息 
  71.    * @param string $content 要回復(fù)的音樂 
  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.    * 回復(fù)圖文信息 
  84.    * @param string $news 要回復(fù)的圖文內(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é)點(diǎn)名稱 
  105.    * @return string 
  106.    */ 
  107.   private function data2xml($xml$data$item = 'item') { 
  108.     foreach ($data as $key => $value) { 
  109.       /* 指定默認(rèn)的數(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ù)進(jìn)行簽名認(rèn)證,確保是微信發(fā)送的數(shù)據(jù) 
  128.    * @param string $token 微信開放平臺設(shè)置的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ù)進(jìn)行字典排序 */ 
  137.     sort($data,SORT_STRING); 
  138.     /* 生成簽名 */ 
  139.     $signature = sha1(implode($data)); 
  140.     return $signature === $sign
  141.   } 

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

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