栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php微信公众平台开发类实例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php微信公众平台开发类实例

本文实例讲述了php微信公众平台开发类。分享给大家供大家参考。具体分析如下:

ThinkWechat.php类文件如下:

auth($token) || exit;
 if(!empty($_GET['echostr'])){
  exit($_GET['echostr']);
 } else {
  try
  {
  $xml = file_get_contents("php://input");
  $xml = new SimpleXMLElement($xml);
  $xml || exit;
  foreach ($xml as $key => $value) {
   $this->data[$key] = strval($value);
  }
  }catch(Exception $e){
  }
 }
 }
 
 public function request(){
 return $this->data;
 }
 
 public function response($content, $type = 'text', $flag = 0){
 
 $this->data = array(
  'ToUserName' => $this->data['FromUserName'],
  'FromUserName' => $this->data['ToUserName'],
  'CreateTime' => time(),
  'MsgType' => $type,
 );
 
 $this->$type($content);
 
 $this->data['FuncFlag'] = $flag;
 
 $xml = new SimpleXMLElement('');
 $this->data2xml($xml, $this->data);
 exit($xml->asXML());
 }
 
 private function text($content){
 $this->data['Content'] = $content;
 }
 
 private function music($music){
 list(
  $music['Title'], 
  $music['Description'], 
  $music['MusicUrl'], 
  $music['HQMusicUrl']
 ) = $music;
 $this->data['Music'] = $music;
 }
 
 private function news($news){
 $articles = array();
 foreach ($news as $key => $value) {
  list(
  $articles[$key]['Title'],
  $articles[$key]['Description'],
  $articles[$key]['PicUrl'],
  $articles[$key]['Url']
  ) = $value;
  if($key >= 9) { break; } //最多只允许10调新闻
 }
 $this->data['ArticleCount'] = count($articles);
 $this->data['Articles'] = $articles;
 }
 
 private function data2xml($xml, $data, $item = 'item') {
 foreach ($data as $key => $value) {
  
  is_numeric($key) && $key = $item;
  
  if(is_array($value) || is_object($value)){
  $child = $xml->addChild($key);
  $this->data2xml($child, $value, $item);
  } else {
  if(is_numeric($value)){
   $child = $xml->addChild($key, $value);
  } else {
   $child = $xml->addChild($key);
   $node = dom_import_simplexml($child);
   $node->appendChild($node->ownerdocument->createCDATASection($value));
  }
  }
 }
 }
 
 private function auth($token){
 if(empty($_GET['signature'])) return;
 
 $data = array($_GET['timestamp'], $_GET['nonce'], $token);
 $sign = $_GET['signature'];
 
 sort($data,SORT_STRING);
 
 $signature = sha1(implode($data));
 return $signature === $sign;
 }
}

希望本文所述对大家的php程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/45770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号