<?php
include_once "wxBizMsgCrypt.php";
//encodingAesKey和token均为申请三方平台是所填写的内容
$encodingAesKey = '';
$token = '';
$appId = '';
$timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']) ;
$nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']) ;
$msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']);
$app_id = $_GET['appid'];
$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
//获取到微信推送过来post数据(xml格式)
$postArr = file_get_contents("php://input");
$msg = '';
$errCode=$pc->decryptMsg($msg_sign, $timeStamp, $nonce, $postArr,$msg);
$postObj=simplexml_load_string($msg,'SimpleXMLElement',LIBXML_NOCDATA);
//第三方平台全网发布检测返回api文本消息测试
if(strpos ($postObj->Content, 'QUERY_AUTH_CODE' ) !== false){
$query_auth_code= str_replace ( 'QUERY_AUTH_CODE:', '', $postObj->Content);
$info= getAuthInfo($query_auth_code);
file_put_contents('lo.txt', '['.date('Y-m-d : h:i:sa',time()).']'.$query_auth_code.'00'.json_encode($info)."\n\r",FILE_APPEND);
$access_info=$info['authorization_info']['authorizer_access_token'];
$param =array();
$param['touser'] = "$postObj->FromUserName";
$param['msgtype'] = 'text';
$param['text']['content'] = $query_auth_code.'_from_api';
$url='https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_info;
$test = post_data($url,json_encode($param));
file_put_contents('lo.txt', '['.date('Y-m-d : h:i:sa',time()).']'.$query_auth_code.'12'.json_encode($param)."\n\r",FILE_APPEND);
file_put_contents('lo.txt', '['.date('Y-m-d : h:i:sa',time()).']'.$query_auth_code.'11'.json_encode($test)."\n\r",FILE_APPEND);
}
function responseText($postObj,$content){
$xml = "<xml>";
$xml.= "<ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName>";
$xml.= "<FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName>";
$xml.= "<CreateTime>".time()."</CreateTime>";
$xml.= "<MsgType><![CDATA[text]]></MsgType>";
$xml.= "<Content><![CDATA[".$content."]]></Content>";
$xml.= "</xml>";
return $xml;
}
function post_data($url,$postdata){
$ch = curl_init(); //用curl发送数据给api
// curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
$response = curl_exec($ch);
curl_close( $ch );
$rest = json_decode($response,true);
return $rest;
}
function getAuthInfo($query_auth_code){
$access_token = '';
$urlAuth = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=$access_token";
$postdata = array(
'component_appid' =>'',
'authorization_code' =>$query_auth_code,
);
$postdata = json_encode($postdata);
$resultAuth = post_data($urlAuth,$postdata);
return $resultAuth;
}
echo $test;
exit();