我正在尝试隐藏我的 API key ,因此由于配额原因无法访问它。 API 仅检索食谱数据,因此不会发生信用卡详细信息等。
自从我对 php 很陌生以来,过去几天我一直在日夜研究这个问题,但似乎 stackoverflow 总体上说需要某种代理。 这是我创建的,我想知道它是否足够安全。
App.js
// I make an axios post request to script.php, passing along a 'searchfield' value. Lets say its "apples"
script.php
// receives the 'searchfield' value, then initiates a curl request to the api with my keycode/searchfield etc.
// This returns the response for apples which is an array of objects back to App.js
我的 script.php 看起来与此类似,我缺少什么吗?
$ch = curl_init();
$url = 'https://fake.com?api_key=1234';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
最佳答案
由于它位于 PHP 端,是一种服务器端语言,并且从外部世界看不到,因此只要您确实有一个用户系统,就可以继续进行安全性工作。如果您不这样做,他们就可以通过调用您的文件来滥用此 api key 。
因此,离开安全方式,明智的做法是将您的 api key 存储为常量,这样在将来的请求中您就不必再次输入它,并且如果您请求更改 api key ,您不必从任何地方更改它。
还有一些很好的用于使用 REST API 的库,您可以在网络上搜索这些库。
关于javascript - 隐藏 api key ,php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50572018/