1,php怎么获取https://api.ipify.org/?format=json
里面的ip(这个链接获取ip不知道是否稳定,有稳定的链接请介绍)。
2、这个ip怎么传入下面的php代码的$ip=‘’里面(这个是站长工具IP查询的的代码)
//填写你的参数
$apikey = 'apiuser_quantity_7119d1ec8faca7761013bfa5abe3da7d_ec6648a6a8a94182946325ee79d6a';
$version = '1.0';
$ip = '61.186.21.203';//目前只能靠手写
$url = sprintf("https://openapi.chinaz.net/v1/1001/ip?APIKey=%s&ChinazVer=%s&ip=%s", $apikey, $version, $ip);
$options = array(
'http' => array(
'method' => 'GET',
'ignore_errors' => true,
'timeout'=>60
)
);
try {
$re = file_get_contents($url, false, stream_context_create($options));
preg_match('/HTTP\/1\.[0|1|x] ([0-9]{3})/', $http_response_header[0], $matches);
$status_code = $matches[1];
printf('返回http状态码:%s', $status_code);
print("\n");
if ($re === false) {
$error = error_get_last();
printf("HTTP请求失败: %s", $error['message']);
} else {
printf('返回结果数据:%s', $re);
}
} catch (Exception $e) {
printf('系统异常: %s', $e->getMessage());
}
3、第2项,ip填入后访问返回的结果如下(上面的key隐藏了些):
返回http状态码:200 返回结果数据:{"StateCode":1,"Reason":"成功","Result":{"IP":"61.186.21.203","City":"海口","Country":"中国","District":"秀英","Isp":"电信","Province":"海南","ZipCode":"570300","AreaCode":"0898"}}
4、怎么在一下的js代码里面获取到php文件返回结果数组里面的参数:City,并用来判断是否放行访问网站或者跳转(用搜狐的地址库代码来改,):
<script type=”text/javascript” src=”http://pv.sohu.com/cityjson?ie=utf-8″></script>//去掉这个,用了上面的站长工具api
<script>
var city = returnCitySN[“cname”];//怎么在这里读取php返回值里面的参数:City
if (city.indexOf(“北京”) >= 0 || city.indexOf(“重庆”) >= 0)//这里是包含则跳转,改为除包含的城市以外都跳转新链接(就是填写的城市不跳转)
{
location.href =”http://baidu.com”;
}
</script>
28日早上,补充下前端代码:这个代码读不出地址的参数:City,帮忙看下哪里出错了
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<title>用站长工具的IP查询API获取城市,判断地区跳转</title>
<script type="text/javascript" src="http://moli.kuaishou.niuka888.com/test/server.php"></script>
</head>
<body>
<input type="hidden" id="city" value="<?php echo $city; ?>">
<script>
var city = document.getElementById("city").value; // 获取城市名参数
if (city.indexOf("北京") >= 0 || city.indexOf("海口") >= 0) {
location.href = "http://www.baidu.com";
}
function myFunction(){
document.getElementById("demo").innerHTML=city;
}
</script>
<p><p><p>
用站长工具的IP查询API获取城市,判断地区跳转
<button onclick="myFunction()">点击这里显示城市</button>
<p id="demo"></p>
</body>
</body>
</html>
```