dpea85385
2019-06-24 11:52
浏览 120
已采纳

更改远程服务器上的JSON文件

I have an api to get some data (limit every 1 minute call) in JSON format and I want that api to be available for all those users who has the access to the server.

I had a php code getting the contents from api then using that JSON object in javascript file. But if multiple users go on the website, the api is called multiple times every minute, so only 1 guy gets the data, and the others don't.

Is there a good solution to something like this?

I try to save the JSON object into a remote file then everyone who accesses the website reads the JSON file which changes every minute but for some reason on the remote server the JSON file isn't being updated every minute. Is it a permission problem?

In php file I use "file_get_contents" to get json from rest api. then I save the data: "file_put_contents"

Is there a different solution of getting the JSON data (updated every 1 minute) to everyone who accesses the website available to use in the javascript file?

How to save json data to remote file every one minute?

图片转代码服务由CSDN问答提供 功能建议

我有一个api来获取JSON格式的一些数据(限制每1分钟调用)我想要api 适用于有权访问服务器的所有用户。

我有一个PHP代码从api获取内容然后在javascript文件中使用 JSON 对象。 但是如果有多个用户访问网站,则每分钟会多次调用api,因此只有1个人获取数据,而其他人则没有。

是否有一个很好的解决方案 这样的事情?

我尝试将 JSON 对象保存到远程文件中,然后访问该网站的每个人都读取 JSON 文件,该文件每分钟都会更改 但由于某些原因,远程服务器上的 JSON 文件并未每分钟更新一次。 这是一个权限问题吗?

在php文件中,我使用“file_get_contents”从rest api获取json。 然后我保存数据:“ file_put_contents“

是否有不同的解决方案可以获取访问可在javascript文件中使用的网站的每个人的JSON数据(每1分钟更新一次)?

如何每隔一分钟将json数据保存到远程文件?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsu0308 2019-06-24 12:42
    已采纳

    How about this:

    • Save your API data in a file.
    • Save your last call for the API data in another file.
    • When users require your data, check for the last API call.
    • If less than 1 minute passed, then get the data from the saved file.
    • If more than a minute passed, get new data, save it to the file, update the time log, then give the data to the user.

    For example, lets say users go to your index.php page:

    // lets define the working files
    define("FILE_API_TIMESTAMP", "api.timestamp.txt");
    define("FILE_API_DATA", "api.data.json");
    // initialize main vars
    // last api call
    $last_call = 0;
    // latest api data
    $api_data = false;
    // this moment timestamp
    $current_time = time();
    // check last api call from saved file
    if (file_exists(FILE_API_TIMESTAMP)) {
      $last_call = floatval( file_get_contents( FILE_API_TIMESTAMP ) );
    }
    // if more than 60 seconds passed since last call,
    // call API then save results
    if ($current_time - $last_call > 60) {
      // get new data
      // $api_data = getApiData(); -> put your function here: Curl etc...
      $api_data = "new data: ".time(); // just an example
      if ($api_data) {
        // if new data is available,
        // - log the current timestamp
        file_put_contents(FILE_API_TIMESTAMP, $current_time);
        // update $last_call for later use
        $last_call = $current_time;
        // - save the new data
        file_put_contents(FILE_API_DATA, $api_data);
      }
    }
    // check if we have new data, if not bring old data
    if (!$api_data) {
      if (file_exists(FILE_API_DATA))
        $api_data = file_get_contents(FILE_API_DATA);
      else
        $api_data = "no_data";
    }
    // finally, give the user the updated data:
    echo "Data updated at: ".date("d-m-Y H:i:s", $last_call);
    echo "<hr/>";
    echo $api_data;
    

    Does this help you?

    点赞 评论

相关推荐 更多相似问题