想把本地文件aip.txt改成远程接口json返回值获取授权ip
怎么写
$ip=$_SERVER['REMOTE_ADDR'];
$ok=0;
foreach (preg_split('/\s+/',file_get_contents('aip.txt')) as $aip)
if ($ip==$aip) $ok=1;
if ($ok==0){
echo '没有';
exit;
想把本地文件aip.txt改成远程接口json返回值获取授权ip
怎么写
$ip=$_SERVER['REMOTE_ADDR'];
$ok=0;
foreach (preg_split('/\s+/',file_get_contents('aip.txt')) as $aip)
if ($ip==$aip) $ok=1;
if ($ok==0){
echo '没有';
exit;
如果要将本地文件 aip.txt 改成远程接口 json 返回值,可以使用 PHP 中的 cURL库。
$ip = $_SERVER['REMOTE_ADDR'];
$url = "远程接口地址";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$ok = 0;
$aips = json_decode($result, true);
foreach ($aips as $aip) {
if ($ip == $aip) {
$ok = 1;
}
}
if ($ok == 0) {
echo '没有';
exit;
}
使用 cURL 向远程接口发送请求,并获取返回的 JSON 数据,将其解码为 PHP 数组,然后遍历该数组,判断当前请求的 IP 是否在该数组中。如果不在,则输出“没有”并退出脚本。