PHP微信开发ReplyModel(封装验证,数据获取,信息返回)

时间:2016-05-17 15:59:00   收藏:0   阅读:296
<?php
class ReplyModel{
//验证token,
public function ValidationToken($token){
if(isset($_GET["echostr"])){
$tokenInfo["token"]=$token;
$tokenInfo["echostr"]=$_GET["echostr"];
$tokenInfo["signature"]=$_GET["signature"];
$tokenInfo["timestamp"]=$_GET["timestamp"];
$tokenInfo["nonce"]=$_GET["nonce"];
if($this->checkSignature($tokenInfo)){
return $tokenInfo["echostr"];
}
return false;
}else{
return true;
}
}
//封装检测函数
private function checkSignature($info)
{
$signature = $info["signature"];
$timestamp = $info["timestamp"];
$nonce = $info["nonce"];
$token = $info["token"];
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}

//得到xml数据并且解析成一个对象,成功返回$postObj,失败返回null
public function getXmlAnalysis(){
//1:获取到微信推送过来的POST数据(xml格式)
$postArr="<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[1]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>";
$postArr=$GLOBALS["HTTP_RAW_POST_DATA"];
/*$postArr="<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[5]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>";*/
if (!empty($postArr)){
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postArr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
return $postObj;
}else{
return null;
}
}

//返回纯文本信息,需要传入的参数为解析后的微信服务器数据对象$postObj,需要返回的内容(eg:$relText="你输入的是数字1,返回的是数字1";)
public function ReplyText($postObj,$relText){
$textArr=array("");
$textArr[‘template‘]=$relText;;
$textArr[‘toUser‘]=$postObj->FromUserName;
$textArr[‘fromUser‘]=$postObj->ToUserName;
$textArr[‘time‘]=time();
$textArr[‘MsgType‘]=‘text‘;
return $textArr;
}

/*返回图文消息,需要传入的参数为解析后的微信服务器数据对象$postObj,需要返回的内容(eg:$relImageText=array(
array("title"=>"pqs",
"description"=>"pqs is user weixin",
"picurl"=>"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png",
"url"=>"http://www.scarecrowpqs.com/atmSystem/")
);;)*/
public function ReplyImageText($postObj,$relImageText){
$textArr=array("");
$textArr[‘toUser‘]=$postObj->FromUserName;
$textArr[‘fromUser‘]=$postObj->ToUserName;
$textArr[‘time‘]=time();
$textArr[‘MsgType‘]=‘news‘;
$textArr[‘template‘]=$relImageText;
return $textArr;
}

//返回图片消息,//返回纯文本信息,需要传入的参数为解析后的微信服务器数据对象$postObj,需要返回的内容(eg:$relText="MediaId";)
public function ReplyImage($postObj,$relImage){
$textArr=array("");
$textArr[‘toUser‘]=$postObj->FromUserName;
$textArr[‘fromUser‘]=$postObj->ToUserName;
$textArr[‘time‘]=time();
$textArr[‘MsgType‘]=‘image‘;
$textArr[‘template‘]=$relImage;
return $textArr;
}

//获取微信接口,需要传入的参数为appid和appsecret,返回值为$accessToken
private function getWxAccessToken($appid,$appsecret){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$arr=$this->http_curl($url);
return $arr;
}

//获取微信接口,判断是否在本地的过期没
public function getIsWxAccessToken(){
if($_SESSION[‘access_token‘] && $_SESSION[‘expires_in‘]>time()){
return $_SESSION[‘access_token‘];
}else{
$relStr=$this->getWxAccessToken("wx9e92e07e63ba07d9","d4624c36b6795d1d99dcf0547af5443d");
$_SESSION["access_token"]=$relStr[‘access_token‘];
$_SESSION["expires_in"]=time()+7200;
return $relStr[‘access_token‘];
}

}

//需要传入的参数为accessToken,返回值为微信服务器地址
public function getWxServerIp($accessToken){
$accessToken=$this->getIsWxAccessToken();
$url="https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken;
$arr=$this->http_curl($url);
return $arr;
}

//curl请求函数
private function http_curl($url,$type=‘get‘,$res=‘json‘,$arr=‘‘,$header=0){
//初始化curl
$ch=curl_init();
//设置curl
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER,$header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//调用接口
if($type==‘post‘){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
}
$rel=curl_exec($ch);
//关闭curl
if(curl_errno($ch)){
var_dump(curl_error($ch));
}else{
curl_close($ch);
if($res==‘json‘){
$relStr=json_decode($rel,true);
return $relStr;
}else{
return $rel;
}
}
}


//定义自定义菜单
public function setMenuItem($MenuArr,$Access_Token){
$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$Access_Token;
$MenuArr=urldecode(json_encode($MenuArr));
$rel=$this->http_curl($url,‘post‘,‘json‘,$MenuArr);
return $rel;
}

}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!