php文件里的代码 谁能给解释一下啊每行什么意思
define("TOKEN", "weixin");
define("MESS","输入点啥吧");
$wechatObj = new wechatCallbackapiTest(); */
$wechatObj->valid();*
//$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0<FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = MESS;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo MESS;
}
}else {
echo MESS;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token =TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?> 展开
define("TOKEN", "weixin");
define("MESS","输入点啥吧");
定义2个常量:TOKEN值为”weixin";MESS值为“输入点啥吧"。
$wechatObj = new wechatCallbackapiTest(); //实例化对象
$wechatObj->valid();
$wechatObj->responseMsg();
变量名->方法名:变芹塌漏册量wechatObj分别调用valid()和responseMsg()方法。
class wechatCallbackapiTest //创建一个类
public function valid() //创建公有方法valid()
public function responseMsg() //创建公有方法responseMsg()
private function checkSignature() //创建私有方法checkSignature()
public function valid()
{
$echoStr = $_GET["echostr"]; //变量echoStr的值为$_GET["echostr"](注释:获取echostr值)
if($this->checkSignature()){ //调用checkSignature()方法
echo $echoStr; //输出$echoStr
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){ //如果变量$postStr非空
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName; //以上不解释
$keyword = trim($postObj->Content); //trim是去除特殊字符
$time = time();
$textTpl = "<xml> //定义变量为xml文本
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0<FuncFlag>
</xml>";
if(!empty( $keyword )) //条件判断,不解释
{
$msgType = "text";
$contentStr = MESS;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo MESS;
}
}else {
echo MESS;
exit;
}
}
private function checkSignature() //不解释
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token =TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
define("MESS"罩悄,"输入点啥吧"); 定义了一个叫 MESS 的常量 值为 输入点啥做闷配纯指吧
$wechatObj = new wechatCallbackapiTest(); */ 实例化了一个叫wechatCallbackapiTest的对象并且起名叫$wechatObj
$wechatObj->valid();* $wechatObj执行了wechatCallbackapiTest类下的valid()方法 后面的*是啥???
//$wechatObj->responseMsg(); 注释掉了$wechatObj执行了wechatCallbackapiTest类下的responseMsg()方法
楼下继续
define("TOKEN", "weixin");//定义1个常量:TOKEN值为”weixin";是开发者任意填写的 笑陆 签名,用于安全验证。
$wechatObj=new wechatCallbackapiTest();//类wechatCallbackapiTest()的实例化。
类wechatCallbackapiTest()中定义了两个方法:
(1)valid()方法是微信服务器接入时的操作。
(2)responseMsg() 方法是接入后的操作,开发时主要在这个方法里修改代 码,实 现微信订阅号的一些功能。
3.$wechatObj->valid();//调用类中碰橡顷的valid()方法
4.if(!empty( $keyword )) //如用户向所关注的订阅号里发送的信息不为空时,所要做的操 作。
{ 如滑
$msgType = "text";
$contentStr = MESS; //向用户回复的内容(例如:$contentStr =“你好!”则只要用户输 入任何内容都会回复“你好!”)
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo MESS;
}
}else {
echo MESS;
exit;
}
5.private function checkSignature() //用于服务器验证所接收的信息是来自于微信服务器
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token =TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}