我的API请求无缘无故地用完25个请求

时间:2018-09-19 20:44:56

标签: php html api

我的请求功能来自PUBG官方API

<?php

    function getProfile($profile, $div){
      $pubgapikey = 'xxxxxxxxxxxxx';
      $id = getID($profile);
      $url = "https://api.pubg.com/shards/pc-na/players/$id/seasons/$div";

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $pubgapikey, 'Accept: application/vnd.api+json'));
      curl_setopt($ch, CURLOPT_URL,$url);
      $result=curl_exec($ch);
      curl_close($ch);

      $json = json_decode($result, true);
      if($json["data"]["type"] == "playerSeason"){
        return $json["data"]["attributes"];
      }else {
        return false;
      }
    }

    function getID($name){
      $pubgapikey = 'xxxxxxxxxxxxxxxxxxx';
      $url = "https://api.pubg.com/shards/pc-na/players?filter[playerNames]=$name";

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $pubgapikey, 'Accept: application/vnd.api+json'));
      curl_setopt($ch, CURLOPT_URL,$url);
      $result=curl_exec($ch);
      curl_close($ch);

      $json = json_decode($result, true);
      return $json["data"][0]["id"];
    }

所以这是我请求数据的功能。我将介绍这种调用方式。

// My index.php file (All requests go through here)    
$page = explode("/", trim($_SERVER["REQUEST_URI"], "/"));

switch($page[0]){
  case "profile":
    require("controllers/search_controller.php");
    $data = getProfile($page[1], "division.bro.official.2018-09");

    if($data != false){
        include("pages/profile.php");
    }else{
        include("pages/home.php");
        echo '<script> document.getElementById("error").innerHTML = "Cannot find user. Remember To Be Capital Sensitive!"; </script>';
    }
    break;
}

我知道我正在使用一种非常愚蠢的方式来包含页面,但没有,但是我不想使用或构建自己的php框架atm,这对我正在做的事情来说很好

// Here is my php for calling the function
<?php
if (isset($_POST['username'])) {
  $user = $_POST['username'];
  if($user != ""){
    header("Location: http://www.statstreak.us/profile/$user");
    die();
  }
}
?>

就是这样。表单只是基本的html表单。

由于某种原因,这会持续消耗我从PUBG收到的每分钟25个请求,这很烦人,因为我找不到一个原因,为什么每个用户要使用超过2个请求

0 个答案:

没有答案