验证token、回复图文\文本、推送消息的实用微信类php代码
本文实例为大家分享了用于验证token,回复图文、文本,向用户推送消息等功能的微信类,具体代码如下
<?php
classWechat{
private$data=array();
publicfunction__construct($token){
$this->auth($token,$wxuser)||exit;
if(IS_GET){
echo($_GET['echostr']);
exit;
}else{
$xml=file_get_contents("php://input");
$xml=newSimpleXMLElement($xml);
//file_put_contents('/var/log/test.txt',$xml,FILE_APPEND);
$xml||exit;
foreach($xmlas$key=>$value){
$this->data[$key]=strval($value);
}
}
}
publicfunctionrequest(){
return$this->data;
}
publicfunctionresponse($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=newSimpleXMLElement('<xml></xml>');
$this->data2xml($xml,$this->data);
exit($xml->asXML());
}
privatefunctiontext($content){
$this->data['Content']=$content;
}
privatefunctionmusic($music){
list($music['Title'],$music['Description'],$music['MusicUrl'],$music['HQMusicUrl'])=$music;
$this->data['Music']=$music;
}
privatefunctionnews($news){
$articles=array();
foreach($newsas$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;
}
privatefunctiontransfer_customer_service($content){
$this->data['Content']='';
}
privatefunctiondata2xml($xml,$data,$item='item'){
foreach($dataas$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));
}
}
}
}
privatefunctionauth($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)){
returntrue;
}else{
returnfalse;
}
returntrue;
}
}
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。