php curl请求两次

时间:2018-09-28 02:29:14

标签: php api curl

我从PUBG的官方API收到的请求数/分钟不得超过25个。由于某种原因,它会为每个搜索请求两次,而不用4个请求。我不知道为什么。我已经检查过代码没有运行两次。仅一次,但仍请求4次。

更新: 我尝试制作一个单独的页面,显然在某处两次调用我的函数存在一个错误。仍然不知道为什么,但是我现在99%确信这不是函数本身。

我的请求代码

function getProfile($profileName, $region, $seasonDate){
  // Just check if there is an acctual user
  if($profileName === null){
    $data->error = "Player Not Found";
    $data->noUser = true;
    return $data;
  }else{
    $season = "division.bro.official.".$seasonDate;

    /*
      Get The UserID
    */

    $ch = curl_init("https://api.pubg.com/shards/$region/players?filter[playerNames]=$profileName");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer APIKEY',
            'Accept: application/vnd.api+json'));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $rawData = json_decode(curl_exec($ch), true);
    $data->playerId = $rawData["data"][0]["id"];
    curl_close($ch);


    // Testing if user exists
    if($rawData["errors"][0]["title"] === "Not Found"){
      $data->noUser = true;
      $data->error = "Player Not Found";
      return $data;
    }else{

    /*
      Get The acctual stats
    */
    $ch = curl_init("https://api.pubg.com/shards/$region/players/$data->playerId/seasons/$season");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer APIKEY',
            'Accept: application/vnd.api+json'));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data->playerDataJSON = curl_exec($ch);
    $data->playerData = json_decode($data->playerDataJSON, true);
    curl_close($ch);

    return $data;
  }
  }
}

这就是它的调用方式

if (isset($_POST['search-username'])) {
  $username = $_POST['search-username'];
  header("Location: /profile/$username/pc-na/2018-01/overall/tpp");
  die();
} 

在实际配置文件中

$data = getProfile($page_parts[1], $page_parts[2], $page_parts[3]);

2 个答案:

答案 0 :(得分:0)

绝对确定它只被调用过一次?设置一个锁。将其更改为

function getProfile($profileName, $region, $seasonDate){
static $once=true;
if($once!==true){
    throw new \LogicException("tried to run getProfile() twice!");
}
$once=false;

答案 1 :(得分:0)

我发现这不是函数之后不久,我才意识到罪魁祸首是我正在调用的空脚本。我知道这个脚本创建了一个我并不在乎的错误,因为它是空的,我也不知道为什么会创建错误。由于某些晦涩的原因,此脚本创建了错误。我将从中吸取教训,以始终修复最小的错误。

相关问题