dounangqie4819 2019-04-19 19:47
浏览 82
已采纳

如何使用此图示实现服务器到服务器数据

I'm setting two virtual hosts on my local pc, the first domain is http://dev.local and the other one handles the api request http://api.server.local/. The idea is simple, but not sure how to implement this kind of setup. So here's the actual process. The dev.local will send some important parameters and values which the API server read it first and validate the data sent from dev.local.

For example I have the API key provided from API server and being stored in the database together with the domain that can only use that API. So the most important thing is that I want to make sure that only dev.local can do the request. Here is some illustration.

[illustration] https://i.imgur.com/OKu34TM.png

I already tried cURL functions but for some reasons, the data can be access by anyone if they have a copy of the api key. So I want to make sure where the request come from or the origin of the request.

This is the script I have for my dev.local in order to get access to my api.server.local

 <?php

 $__apiServer    = 'http://api.server.local';
 $__apiVersion   = '1.0';
 $__apikey       = '7c4a8d09ca3762af61e59520943dc26494f8941b'; // API Key
 $__apiEmail     = 'johnsmith99@gmail.com'; // Registered Email Address
 $__apiUser      = 'johnsmith'; // Username

 $__curlURL      = "";
 $__curlURL      = "{$__curlURL}{$__apiServer}/v{$__apiVersion}";
 $__curlURL      = "{$__curlURL}/bin.php?user={$__apiUser}";
 $__curlURL      = "{$__curlURL}&email={$__apiEmail}";
 $__curlURL      = "{$__curlURL}&key=$__apikey";
 $__curlURL      = "{$__curlURL}&domain=$_SERVER[SERVER_NAME]";

 $ch = curl_init();

 curl_setopt($ch, CURLOPT_URL, $__curlURL);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt($ch, CURLOPT_TIMEOUT, 30);         

 if(curl_exec($ch) === FALSE) {

      echo "Failed to load resource files from the API Server: $__apiServer";

 } else {

     $__curlURL = curl_exec($ch);

      if($err != 1){

         eval(' ?>' . $__curlURL);

      }
 }

 curl_close($ch);

I expect that the value can only be return if the required data are valid. For now the ouput can be read as expected but can be accessible by anyone if they have the copy of api key and other credentials.

  • 写回答

1条回答 默认 最新

  • doudi8231 2019-05-02 18:18
    关注

    I figure it out. I used cURL POST method and it is more secure than using GET. And parse array variables to validate the main parameters.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法