doutuo7156 2018-05-28 18:43 采纳率: 100%
浏览 65
已采纳

隐藏api密钥,php

I'm trying to hide my api key, so it is no accessible for quota reasons. The api just retrieves data that is recipes, so no credit card details etc is going on.

Ive been working on this for the past few days all day/night since Im pretty new to php, but it seemed stackoverflow in general was saying there needs to be some sort of proxy. This is what I created and I would like to know if it is secure enough.

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

my script.php looks along the lines of this, anything Im missing?

$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);
  • 写回答

1条回答 默认 最新

  • douyinzha5820 2018-05-28 18:52
    关注

    Since it is on the PHP Side of things which is a Server Side Language and is not viewable from the outside world you are good to go on the security thing as long you do have a user system in place. If you don't they can abuse this api key by just calling your file.

    So leaving the security way of things, it would be wise to store your api key to a constant, so on future requests you won't have to type it again, and in case you request a change of your api key, you won't have to change it from everywhere.

    There are also good libraries for consuming REST APIs, which you can search on the web.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。