代码拉取完成,页面将自动刷新
<?php
/**
* Created by PhpStorm
* @Date: 2017/7/13
* @Time: 10:59
* @Author: eRic
* @Email: [email protected]
* @File: notify_url.php
*/
include_once "../application/third_party/Dsanfang-php-msg/wxBizMsgCrypt.php";
require_once '../vendor/autoload.php';
$options = include_once "../application/config/wechat.php";
use EasyWeChat\Foundation\Application;
use EasyWeChat\OpenPlatform\Guard;
use EasyWeChat\OpenPlatform;
function Curl_Post($url,$data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function Curl_Get($url){
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}
/**
* curl POST
*
* @param string url
* @param array 数据
* @param int 请求超时时间
* @param bool HTTPS时是否进行严格认证
* @return string
*/
function curlPost($url, $data = array(), $timeout = 30, $CA = false){
//$cacert = getcwd() . '/cacert.pem'; //CA根证书
$SSL = substr($url, 0, 8) == "https://" ? true : false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout-2);
if ($SSL && $CA) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
//curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
} else if ($SSL && !$CA) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode
$ret = curl_exec($ch);
//var_dump(curl_error($ch)); //查看报错信息
curl_close($ch);
return $ret;
}
function component_access_token($options){
$url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
$data = [
'component_appid'=> $options['app_id'], //'wx2fd841a039df1eea',
'component_appsecret' => $options['secret'], //'800cd9f6c52318baeecb5562ce1175a6',
'component_verify_ticket'=> 'ticket@@@PQCwywpDZsZKXOp7HtIKh7CPC4BRHJ1WAlk1QcYbRALdsmDJF8JjeerH6SNg-MDmLWx4DBpT5D76Je6lZ4h6OA'
];
$data = json_encode( $data );
$ret = curlPost( $url, $data );
return $ret;
}
function pre_auth_code($options){
$url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=";
$url .= "3WuYhUm-3AqjpLJJ0N2Xv50CUgAnmIr4ixQRyL48--KM31_uWQncCiTvvjlwlFk6mZFK5UgH-ComQ0OgrhIeZMltIcV27OJ65X6p5HO5TbPZ3TVdrIA6G5PKiF9mApVEPASfACAJUM";
$data = [
'component_appid'=> $options['app_id'],
];
$data = json_encode( $data );
$ret = curlPost( $url, $data );
return $ret;
}
function authorizer_refresh_token($options,$code){
$url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=";
$url .= "3WuYhUm-3AqjpLJJ0N2Xv50CUgAnmIr4ixQRyL48--KM31_uWQncCiTvvjlwlFk6mZFK5UgH-ComQ0OgrhIeZMltIcV27OJ65X6p5HO5TbPZ3TVdrIA6G5PKiF9mApVEPASfACAJUM";
$data = [
'component_appid'=> $options['app_id'],
'authorization_code'=> $code,
];
$data = json_encode( $data );
$ret = curlPost( $url, $data );
return $ret;
}
/**
* xml转换成数组
* @param $xml
* @return mixed
* author Fox
*/
function xmlToArray($xml)
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
//$AuthorizationCode = "queryauthcode@@@9AgyMPtjn5UmI4yJ0AMO3vluFQM0NhODm5uq_AcwpUyoqAU5oClGNaRYeTTOc9VuOy75BpgJLH9TpdCXjKlejA";
$ret = component_access_token($options);
$cdoes =(array)json_decode($ret);
file_put_contents('data.txt',$cdoes['component_access_token']);
/*$AuthorizationCode = "queryauthcode@@@bf6jbdOEns-wXUPWWjUSRdfZOKS9GFhdYFHVhhvu2RRvHQIYh5dKKRdNBqq9XZtIGy1D1yKdNxvZKecAPPAgjA";
$ret = authorizer_refresh_token($options,$AuthorizationCode);
echo "<pre>";
print_r($ret);
echo "</pre>";
exit;
exit;*/
//----------------------------------------
/*$encodingAesKey = "XAgF708iKiC6zjYcbmhusltyQy5FTVrS7BHjlNM6wUh";
$token = "708iKiC6zjYcbmhusltyQy5FTVr";
$appId = "wx2fd841a039df1eea";
$timeStamp = empty ( $_GET ['timestamp'] ) ? '' : trim ( $_GET ['timestamp'] );
$nonce = empty ( $_GET ['nonce'] ) ? '' : trim ( $_GET ['nonce'] );
$msg_sign = empty ( $_GET ['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] );
$encryptMsg = file_get_contents ( 'php://input' );
$pc = new WXBizMsgCrypt ( $token, $encodingAesKey, $appId);
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$array_s = $xml_tree->getElementsByTagName('MsgSignature');
$encrypt = $array_e->item(0)->nodeValue;
$msgsd_sign = $array_s->item(0)->nodeValue;
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf ($format,$encrypt);
$msg = '';
$authsuccess = '';//解密后的数组
$errCode = $pc->decryptMsg ( $msg_sign, $timeStamp, $nonce, $from_xml, $msg ); // 解密
$contarr['errCode'] = $errCode;
$contarr['time'] = time();
$param_arr = '';
if ($errCode == 0) {
$authsuccess = xmlToArray($msg);
$param = $authsuccess;
switch ($param ['InfoType']) {
case 'component_verify_ticket' : // 授权凭证
$park = file_get_contents('data.txt');
if($park){
$park = unserialize($park);
$park['component_verify_ticket'] = $param;
}else{
$param_arr['component_verify_ticket'] = $param;
file_put_contents ( 'data.txt', serialize($param_arr) ); // 缓存
}
break;
case 'unauthorized' : // 取消授权
$park = file_get_contents('data.txt');
if($park){
$park = unserialize($park);
$park['unauthorized'] = $param;
}else{
$param_arr['unauthorized'] = $param;
file_put_contents ( 'data.txt', serialize($param_arr) ); // 缓存
}
$status = 2;
break;
case 'authorized' : // 授权
$park = file_get_contents('data.txt');
if($park){
$park = unserialize($park);
$park['authorized'] = $param;
}else{
$param_arr['authorized'] = $param;
file_put_contents ( 'data.txt', serialize($param_arr) ); // 缓存
}
$status = 1;
break;
case 'updateauthorized' : // 更新授权
$park = file_get_contents('data.txt');
if($park){
$park = unserialize($park);
$park['updateauthorized'] = $param;
}else{
$param_arr['updateauthorized'] = $param;
file_put_contents ( 'data.txt', serialize($param_arr) ); // 缓存
}
break;
}
}
exit;*/
/*$pre_auth_code = "preauthcode@@@NVTRRC_yzCht0-s6MTX3r_8XGdAeseRthd8YLgCabkQLBICbb8XX3d0S8OUJs54n";
$redirect_uri = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/authnotify_url.php";
echo '<!doctype html><html ><head><title>Document</title></head><body><div><br/><br/><br/>123456123456<br/><a href="https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=' . $options['app_id'] . '&pre_auth_code=' . $pre_auth_code . '&redirect_uri=' . $redirect_uri . '">点击授权</a></div></body></html>';*/
//$ret = pre_auth_code($options);
/*echo "<pre>";
print_r($ret);
echo "</pre>";*/
$encodingAesKey = "XAgF708iKiC6zjYcbmhusltyQy5FTVrS7BHjlNM6wUh";
$token = "708iKiC6zjYcbmhusltyQy5FTVr";
$appId = "wx2fd841a039df1eea";
$encryptMsg = file_get_contents ( 'php://input' );
//------解密----------
$contarr = [];
$timeStamp = empty ( $_GET ['timestamp'] ) ? '' : trim ( $_GET ['timestamp'] );
$nonce = empty ( $_GET ['nonce'] ) ? '' : trim ( $_GET ['nonce'] );
$msg_sign = empty ( $_GET ['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] );
$encryptMsg = file_get_contents ( 'php://input' );
$pc = new WXBizMsgCrypt ( $token, $encodingAesKey, $appId);
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$array_s = $xml_tree->getElementsByTagName('MsgSignature');
$encrypt = $array_e->item(0)->nodeValue;
$msgsd_sign = $array_s->item(0)->nodeValue;
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf ($format,$encrypt);
$msg = '';
$authsuccess = '';//解密后的数组
$errCode = $pc->decryptMsg ( $msg_sign, $timeStamp, $nonce, $from_xml, $msg ); // 解密
$contarr['errCode'] = $errCode;
$contarr['time'] = time();
if ($errCode == 0) {
$contarr['msg'] = $msg;
$authsuccess = xmlToArray($msg);
$contarr['msg_jiemi'] = $authsuccess;
}
error_log(print_r($contarr,true),3,'log.txt');
exit;
//-------end 解密---------
$app = new Application($options);
$openPlatform = $app->open_platform;
$server = $openPlatform->server;
try{
if(isset($contarr['msg_jiemi']['AuthorizationCode'])) {
$QUERY_AUTH_CODE = explode('@@@',$contarr['msg_jiemi']['AuthorizationCode']); //"queryauthcode@@@vDkMRyc_d2-Ne2A_jbnxxkI185_dB3aBru42qqrOodULPvp_zkAAB67XATRqTfjd4Ums8VblawwP4Zh8VwgFGA";
file_put_contents('data.txt',serialize($contarr['msg_jiemi']));
}
if(isset($contarr['msg_jiemi']['InfoType']) && $contarr['msg_jiemi']['InfoType'] == "component_verify_ticket"){
$AuthorizationCode_info = file_get_contents('data.txt');
$AuthorizationCode_info = unserialize($AuthorizationCode_info);
$contarr['AuthorizationInfo111'] = $openPlatform->getAuthorizationInfo($AuthorizationCode_info['AuthorizationCode']);
error_log(print_r($contarr,true),3,'log.txt');
$server->setMessageHandler(function ($event) use ($openPlatform) {
// 事件类型常量定义在 \EasyWeChat\OpenPlatform\Guard 类里
switch ($event->InfoType) {
case Guard::EVENT_AUTHORIZED: // 授权成功
$authorizationInfo = $openPlatform->getAuthorizationInfo($event->AuthorizationCode);
error_log(print_r($authorizationInfo,true),3,'log.txt');
// 保存数据库操作等...
case Guard::EVENT_UPDATE_AUTHORIZED: // 更新授权
// 更新数据库操作等...
case Guard::EVENT_UNAUTHORIZED: // 授权取消
// 更新数据库操作等...
}
});
$response = $server->serve();
$response->send();
}
}catch(Exception $e){
error_log(print_r($e->getMessage(),true),3,'log.txt');
}
error_log(print_r($contarr,true),3,'log.txt');
//$message = $server->getMessage();
//error_log(print_r($message['ToUserName'],true),3,'log.txt');
/*$server->setMessageHandler(function($event) use ($openPlatform) {
// 事件类型常量定义在 \EasyWeChat\OpenPlatform\Guard 类里
switch ($event->InfoType) {
case Guard::EVENT_AUTHORIZED: // 授权成功
$authorizationInfo = $openPlatform->getAuthorizationInfo($event->AuthorizationCode);
error_log(print_r(array('authorizationInfo'=>$authorizationInfo),true),3,'log.txt');
// 保存数据库操作等...
case Guard::EVENT_UPDATE_AUTHORIZED: // 更新授权
// 更新数据库操作等...
case Guard::EVENT_UNAUTHORIZED: // 授权取消
// 更新数据库操作等...
}
});
$response = $server->serve();
$response->send();*/
/*$pre_auth = $openPlatform->pre_auth->getCode();
// 直接跳转
$response = $openPlatform->pre_auth->redirect('{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/authnotify_url.php');
// 获取跳转的 URL
$pre_auth_url = $response->getTargetUrl();*/
echo "success";
exit;
function xmlde($file){
$xml_tree = new DOMDocument();
$xml_tree->load($file);
$postDom = $xml_tree ->getElementsByTagName("post");
$arr = [];
foreach($postDom as $key =>$val){
$id = $val->getElementsByTagName("title");
$arr[$key]['id'] = $id ->item(0)->attributes->item(0)->nodeValue;
$title = $val->getElementsByTagName("title");
$arr[$key]['title'] = $title->item(0)->nodeValue;
$details = $val->getElementsByTagName("details");
$arr[$key]['details'] = $details->item(0)->nodeValue;
}
return $arr;
}
$xml_text = "<xml>
<AppId><![CDATA[wx2fd841a039df1eea]]></AppId>
<Encrypt><![CDATA[o0OTvKrrPTTU7t04LvEbMKjnPuOUgEFYURAeBPpI+JAEDr5vfyUHKCNLvKh4ZIHh+dXUw2v1Lpv+vKmkjuR2VANEERX/0q89htxhI4LjHlYcH0DkamXJjBqOvkZKK4/LgdPVUK81V76p2ntyvFUEvq01/OBTroWQzdcj6C+HhGUebltMB3r+EaBIinv+4eaJRn0zkHd3WQDtw5isZEmBYxkHjvFZn7rZXZgW4Lt/nlfT129NKfDPk+XLmUErHtsH/3/ktzGmZu+XRcziVIdFJdLfuuxJSOcjC+8IjBlu2w88/DG/DfW/5ats3kgpVdYUtFyUZkRh0vygb3+skZrb3uOcg2v0YIz2rePbTq9P4DQInqVRUJbnhPtdQPsuprzzet9qyZCo+etK6kb5zuBrONMNDeIEwb9zP+4jn8tbd4P9eXuXxBXEY8PnE+d3ZC7AL2qakzkdPuGxfMu7Z6TAWw==]]></Encrypt>
</xml>";
$encodingAesKey = "XAgF708iKiC6zjYcbmhusltyQy5FTVrS7BHjlNM6wUh";
$token = "708iKiC6zjYcbmhusltyQy5FTVr";
$appId = "wx2fd841a039df1eea";
$arr = "";
$timeStamp = empty ( $_GET ['timestamp'] ) ? '' : trim ( $_GET ['timestamp'] );
$nonce = empty ( $_GET ['nonce'] ) ? '' : trim ( $_GET ['nonce'] );
$msg_sign = empty ( $_GET ['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] );
$encryptMsg = file_get_contents ( 'php://input' );
$arr['encryptMsg'] = $encryptMsg;
$pc = new WXBizMsgCrypt ( $token, $encodingAesKey, $appId);
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$array_s = $xml_tree->getElementsByTagName('MsgSignature');
$encrypt = $array_e->item(0)->nodeValue;
$msgsd_sign = $array_s->item(0)->nodeValue;
$arr['encrypt'] = $encrypt;
$arr['msgsd_sign'] = $msgsd_sign;
//$postArr = ArrayUtil::xml2array ( $encryptMsg ); // xml对象解析
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf ($format,$encrypt);
// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $pc->decryptMsg ( $msg_sign, $timeStamp, $nonce, $from_xml, $msg ); // 解密
if ($errCode == 0) {
$param = xmlToArray($msg);
switch ($param ['InfoType']) {
case 'component_verify_ticket' : // 授权凭证
$component_verify_ticket = $param ['ComponentVerifyTicket'];
$ret ['component_verify_ticket'] = $component_verify_ticket;
file_put_contents ( OPEN_COMPONENT_VERIFY_TICKET_PATH, $component_verify_ticket ); // 缓存
break;
case 'unauthorized' : // 取消授权
$status = 2;
break;
case 'authorized' : // 授权
$status = 1;
break;
case 'updateauthorized' : // 更新授权
break;
}
}
$_REQUEST['auth'] = "123";
$_REQUEST['file'] = $_FILES;
error_log(print_r($_REQUEST,true),3,'log.txt');
echo "success";
?>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。