[英]How can in_array work while fetching values from external URL using PHP
I have following code which contains some keys and when someone want to get some info from my server they send that API param with the URL and then it validates with my API keys stored and returns output.
$get_api = $_GET['api'];
$api = array('api_key1','api_key2','api_key3','api_key4');
if(in_array($get_api,$api, true)){
echo "Found";
} else{
echo "Not found";
}
当有人通过found
参数时,他们将看到 output 如下所示:
https://www.example.com/index.php?api=api_key1
但是我有几台服务器,我必须在其中托管这些 API 密钥。 所以我不能手动 go 并在每次必须添加一个时添加 API 键。 所以我做了以下事情。 我尝试在一台服务器上托管 API 密钥,其他所有服务器都会查看该文件,如果找到它会返回found
。
代码是:
API 托管代码:(HTML)
'api_key1','api_key2','api_key3','api_key4'
请求 API 密钥的代码:(PHP)
$get_api = $_GET['api'];
$fetch_keys = file_get_contents("https://www.example.com/path-to-keys.html");
$api = array($fetch_keys); // fetching and putting that in array
if(in_array($get_api,$api, true)){
echo "Found";
} else{
echo "Not found";
}
但是,它不起作用。 它返回内部错误。 请任何人都可以指导我如何解决它,或者他们有更好的方法来解决它。 谢谢
试试 $api=explode (',', $fetch_keys);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.