验证token、回复图文文本、推送消息的实用微信类php代码
程序员文章站
2023-12-09 22:04:52
本文实例为大家分享了用于验证token,回复图文、文本,向用户推送消息等功能的微信类,具体代码如下
本文实例为大家分享了用于验证token,回复图文、文本,向用户推送消息等功能的微信类,具体代码如下
<?php class wechat{ private $data = array(); public function __construct($token){ $this -> auth($token, $wxuser) || exit; if(is_get){ echo($_get['echostr']); exit; }else{ $xml = file_get_contents("php://input"); $xml = new simplexmlelement($xml); //file_put_contents('/var/log/test.txt', $xml,file_append); $xml || exit; foreach ($xml as $key => $value){ $this -> data[$key] = strval($value); } } } 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' => now_time, 'msgtype' => $type); $this -> $type($content); $this -> data['funcflag'] = $flag; $xml = new simplexmlelement('<xml></xml>'); $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; } } $this -> data['articlecount'] = count($articles); $this -> data['articles'] = $articles; } private function transfer_customer_service($content){ $this -> data['content'] = ''; } 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){ $signature = $_get["signature"]; $timestamp = $_get["timestamp"]; $nonce = $_get["nonce"]; $tmparr = array($token, $timestamp, $nonce); sort($tmparr, sort_string); $tmpstr = implode($tmparr); $tmpstr = sha1($tmpstr); if(trim($tmpstr) == trim($signature)){ return true; }else{ return false; } return true; } } ?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。