隐藏 api 键,php

Hiding an api key, php

我试图隐藏我的 api 密钥,因此由于配额原因无法访问它。 api 只是检索食谱数据,因此没有信用卡详细信息等。

过去几天我一直在研究这个 day/night,因为我对 php 还很陌生,但似乎 Whosebug 总体上说需要某种代理。 这是我创建的,我想知道它是否足够安全。

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 密钥。

因此,为了安全起见,将 api 密钥存储为常量是明智的,这样在以后的请求中您就不必再次输入它,如果您请求更改您的 api 密钥,您不必从任何地方更改它。

还有一些很好的使用 REST API 的库,您可以在网上搜索。