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

首頁 > 開發 > PHP > 正文

php微信公眾平臺開發類實例

2024-05-04 23:33:44
字體:
來源:轉載
供稿:網友

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

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

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

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泽州县| 韩城市| 浪卡子县| 阆中市| 凤山市| 轮台县| 涞源县| 德州市| 扶余县| 托克托县| 安陆市| 秦皇岛市| 金川县| 吉首市| 肇庆市| 绍兴市| 永定县| 芮城县| 镇雄县| 偃师市| 乌鲁木齐县| 镇平县| 墨玉县| 缙云县| 金昌市| 米泉市| 和静县| 凤山市| 遵义县| 贵阳市| 阳谷县| 定襄县| 昭平县| 平舆县| 屏边| 伊川县| 怀集县| 株洲县| 吉隆县| 玉溪市| 天峨县|