您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP之微信公众号验证token、回复内容、推送消息的方法

2024/3/9 14:02:42发布26次查看
本文主要为大家详细介绍了php代码实现验证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; } } ?>
相关推荐:
tp接入微信公众号支付详解
微信公众号开发配置常见错误信息汇总
微信公众号实现用户管理功能
以上就是php之微信公众号验证token、回复内容、推送消息的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product