20,398
社区成员




<?php
// wechat php
//define your token定义接口
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj ->GetImageMsg();//这个要调用自动回复消息!!
$wechatObj->valid();
class wechatCallbackapiTest
{
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>接口验证>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
header('content-type:text');//在微信样本代码上添加了如此语句,因而接口接成功
echo $echoStr;
exit;
}
}
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>自动回复消息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public function GetImageMsg()//自动回复消息
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr))
{
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$msgType=$postObj->$MsgType;
$picUrl=trim($postObj->PicUrl);
$mediaId=trim($postObj->MediaId);
$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(strtolower($msgType) =="image")
{
if(!empty($picUrl)){
$msgType ="text";
$contentStr = "图片链接:".$picUrl."\n";
$contentStr =$contentStr."媒体ID:".$mediaId;
}else{
$contentStr = "请发送图片哦";
}
}else{
$msgType ="text";
$contentStr = "我只接收图片消息";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
else
{
echo "";
exit;
}
}
}
?>