第一种方法食用方式
<?php
/** ----------------------------------
* 授权JSON服务端
-------------------------------------- */
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
$domain = $_GET['domain'];
$json = array('baidu.com'); //数组
if(in_array($domain, $json)){ //in_array匹配
$value = array(
'code'=>200,
'data'=>'当前域名已授权'
);
}else{
$value = array(
'code'=>202,
'data'=>'域名未授权'
);
}
echo json_encode($value,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
/** ----------------------------------
* 授权客户端
-------------------------------------- */
$host = $_SERVER["HTTP_HOST"];
$file = file_get_contents('http://域名/api.php?domain='.$host);
$data = json_decode($file,true);
if(!empty($data['code'] == 202)){
die("未授权!"); //提示
}
第二种多维数组验证食用方式
实例采用域名+时间验证
<?php
/** ----------------------------------
* 授权JSON服务端
-------------------------------------- */
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
$domain = $_GET['domain'];
//二维数组
$data=array (
'api' => array (
0 => array ( 'ytime' => '2020-05-10 18:43:20', 'ntime' => '2020-12-10','url' => 'baidu.com'),
1 => array ( 'ytime' => '2020-05-10 18:43:20','ntime' => '2020-12-10','url' => 'baidu.com'),
),
);
$pora = start($domain,$data['api']);
if($pora['domain'] == $domain){
$ceil = ceil((strtotime($pora['end']) - strtotime($pora['start']))/86400);
if($ceil == 0){
$item = array('code'=>202,'msg'=>'授权时间到期');
}else{
$item = $value;
}
}else{
$item = array('code'=>202,'msg'=>'未授权');
}
echo json_encode($item,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
function start($url,$api){
$filter = array_filter($api, function($vue) use ( $url){
if($vue["url"] == $url){return $vue['url'] == $url;}
});
foreach ($filter as $getsort){
$type = array('domain'=>$getsort['url'],'start'=>$getsort['ytime'],'end'=>$getsort['ntime']);
}
return $type; // 函数返回内容
}
/** ----------------------------------
* 授权客户端
-------------------------------------- */
$host = $_SERVER["HTTP_HOST"];
$file = file_get_contents('http://域名/api.php?domain='.$host);
$data = json_decode($file,true);
if(!empty($data['code'] == 202)){
die($data['msg']); //提示
}