/*
方倍工作室 http://www.cnblogs.com/txw1958/
CopyRight 2013 www.fangbei.org All Rights Reserved
*/
header('Content-type:text');
//define("TOKEN", "weixin");
define("TOKEN","molihua");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
//回复文本消息(收到文本信息回复OK)
// $wechatObj->responseMsg();
//回复图片消息
//$wechatObj->catchPic();
//回复关注事件
//$wechatObj->attention();
//$wechatObj->responseTestInfo();
//回复单图文
//$wechatObj->singlePic();
//菜单设置
// $wechatObj->menuSet();
//获得access_token
// $wechatObj->getToken();
//回复地理位置OK
// $wechatObj->location();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//记住这才是真正的XML字符串
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
0
";
if($keyword == "?" || $keyword == "?")
{
$msgType = "text";
$contentStr = date("Y-m-d H:i:s",time());
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else{
echo "";
exit;
}
}
//用户发送图片,注意,这个不行,需要mediaID,上传素材才有mediaID
/* public function catchPic()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('pic.txt',12345);
if (!empty($postStr)){
file_put_contents('pc.txt',12345);
$postObj = simplexml_load_string($postStr);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
// $keyword = trim($postObj->Content);
$time = time();
$textTpl="
%s
";
$msgType = "image";
$mediaId=trim($postObj->MediaId);
$picurl='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1495194853502&di=f4c31d1b1ea9df864e8e3ebd15e70189&imgtype=0&src=http%3A%2F%2Fnews.xinhuanet.com%2Fhealth%2F2010-08%2F18%2F12460007_491n.jpg';
$contentStr = date("Y-m-d H:i:s",time());
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$picurl, $mediaId);
echo $resultStr;
}else{
echo "";
exit;
}
} */
//关注事件 OK
public function attention()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('nima.txt',32141);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if(strtolower($postObj->MsgType)=='event'){
if(strtolower($postObj->Event)=='subscribe'){
//发给用户
$toUser=$postObj->FromUserName;
//谁发
$fromUser=$postObj->ToUserName;
$time=time();
$MsgType='text';
$content='weclome';
$textTpl="
%s
";
$result = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(),$MsgType, $content);
echo $result;
}
}
}
//接收文本信息,回复文本信息
public function responseTestInfo()
{
//用户的openID是根据用户的ID和公众号的ID加密出来的
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$textTpl="
%s
";
if(strtolower($postObj->MsgType)=='text'){
file_put_contents('responsetext.txt',321416778878);
$toUser=$postObj->FromUserName;
//谁发
$fromUser=$postObj->ToUserName;
$time=time();
$MsgType='text';
$content='this is text';
$result = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(),$MsgType, $content);
echo $result;
}
}
//单图文 OK
public function singlePic()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
file_put_contents('diyi.txt',1431);
if(strtolower($postObj->MsgType)=='text'){
file_put_contents('dier.txt',1431);
$toUser=$postObj->FromUserName;
//谁发
$fromUser=$postObj->ToUserName;
$textTpl="
%s
1
";
$time=time();
$MsgType='news';
$title='这是title';
$description='这是description';
$picurl='http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg';
$url='https://www.baidu.com/';
$result = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(),$MsgType, $title,$description,$picurl,$url);
echo $result;
}else {
file_put_contents('disan.txt',1431);
$textTpl="
%s
";
$MsgType='text';
$content='this is text';
$result = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(),$MsgType, $content);
echo $result;
}
}
//获取access_token
public function getToken()
{
//1.请求url地址
$appid = 'wx800913eefbf9e5a0';
$appsecret = '3319d9221fbfbb35aac10a74cc9934fe';
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
echo $this->curl_request($url);
}
//自定义菜单
public function menuSet()
{
}
//地理位置消息
public function location()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('gun.txt',$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$toUserName=trim($postObj->ToUserName);
$FromUserName=trim($postObj->FromUserName);
$MsgType=trim($postObj->MsgType);
$Location_X=trim($postObj->Location_X);
$Location_Y=trim($postObj->Location_Y);
$Scale=trim($postObj->Scale);
$Label=trim($postObj->Label);
//$content='helo world';
$content='您的坐标经度是'.$Location_X.'纬度是'.$Location_Y;
if(strtolower($MsgType)=='location'){
$textTpl="
%s
";
$result = sprintf($textTpl, $FromUserName, $toUserName, time(),'text', $content);
echo $result;
}else {
echo '';
exit;
}
}
/**
*_request():发出请求
*@curl:访问的URL
*@https:安全访问协议
*@method:请求的方式,默认为get
*@data:post方式请求时上传的数据
**/
public function curl_request($curl, $https=true, $method='get', $data=null){
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $curl);//设置访问的URL
curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容,但不输出
if($https){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不做服务器认证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不做客户端认证
}
if($method == 'post'){
curl_setopt($ch, CURLOPT_POST, true);//设置请求是POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置POST请求的数据
}
$str = curl_exec($ch);//执行访问,返回结果
curl_close($ch);//关闭curl,释放资源
return $str;
}
}
?>
一键复制
编辑
Web IDE
原始数据
按行查看
历史