+发表新主题
查看: 1552|回复: 0

[开发入门] 开发类实例

[复制链接]

[开发入门] 开发类实例

[复制链接]
风的传说自由 发表于 2016-1-18 15:49:00 浏览:  1552 回复:  0 [显示全部楼层] 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本文实例讲述了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(!empty($_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.    * * 响应微信发送的信息(自动回复)
  38.    * @param string$to   接收用户名
  39.    * @param string$from  发送者用户名
  40.    * @param array$content 回复信息,文本信息为string类型
  41.    * @param string$type  消息类型
  42.    * @param string$flag  是否新标刚接受到的信息
  43.    * @returnstring     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.     /* 添加状态 */
  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 数字索引时的节点名称
  105.    * @returnstring
  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.    * 对数据进行签名认证,确保是微信发送的数据
  128.    * @param string$token 微信开放平台设置的TOKEN
  129.    * @returnboolean    true-签名正确,false-签名错误
  130.    */
  131.   private function auth($token){
  132.     if(empty($_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.   }
  142. }
复制代码





文所述对大家的php程序设计有所帮助。
回复

使用道具 举报


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 赣南网

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4

用微信扫一扫

赣南网