|
本文实例为大家分享了用于验证token,回复图文、文本,向用户推送消息等功能的微信类,具体代码如下
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('');
$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;
}
}
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:php实现发送微信模板消息的方法基于php的微信公众平台开发入门实例关于php微信订阅号开发之token验证后自动发送消息给订阅号但是没有消息返回的问题PHP微信公众号自动发送红包APIphp版微信公众平台实现预约提交后发送email的方法php 微信公众平台开发模式实现多客服的实例代码php微信公众号开发模式详解PHP实现微信模拟登陆并给用户发送消息的方法【文字,图片,图文】PHP实现微信公众号企业号自定义菜单接口示例PHP memcache在微信公众平台的应用方法示例PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
|
|