简体   繁体   English

php curl 请求两次

[英]php curl requesting twice

I have a limit of 25 requests/min from PUBGs official API. For some reason instead of it requesting twice for each search its using up 4 requests.我从 PUBGs 官方 API 限制了 25 个请求/分钟。出于某种原因,它没有为每次搜索请求两次,而是用完了 4 个请求。 I can't figure out why.我不知道为什么。 I have checked that the code isn't running twice.我检查过代码没有运行两次。 Only once, but still it's requesting 4 times.只有一次,但它仍然请求 4 次。

UPDATE: I tried making a separate page and apparently there is a bug somewhere calling my function twice.更新:我尝试制作一个单独的页面,显然某处有一个错误调用我的 function 两次。 Still don't know why but I'm now 99% sure it's not the function itself.仍然不知道为什么,但我现在 99% 确定它不是 function 本身。

Code For My Request我的请求代码

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;
  }
  }
}

This is how it's getting called这就是它被调用的方式

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

In The actual profile php在实际配置文件 php

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

absolutely sure it's only called once? 绝对确定它只被调用过一次? set a lock on it. 设置一个锁。 change it to 更改为

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

Shortly after I figure out it's not the function I realized that the culprit was an empty script I was calling. 我弄清楚这不是函数之后不久,我才意识到罪魁祸首是我正在调用的空脚本。 I knew this script created an error which I didn't really care about since it was empty and I had no idea why it was creating the error. 我知道这个脚本创建了一个我并不在乎的错误,因为它是空的,我也不知道为什么会创建错误。 For some obscure reason this script created the error. 由于某些晦涩的原因,此脚本创建了错误。 I'll just make a lesson out of it to always fix the smallest errors. 我将从中吸取教训,以始终修复最小的错误。

I've also been seeing this behavior - a script with a single curl_exec() request gets called twice.我也看到过这种行为——带有单个 curl_exec() 请求的脚本被调用了两次。 The strange thing is this was only happening when running on localhost (under a wampp installation) but when run from any other webserver was fine and it is just called once.奇怪的是,这只在本地主机上运行时发生(在 wampp 安装下),但是从任何其他网络服务器运行时都很好,它只被调用一次。

I never managed to debug it completely but it seems to be an issue with the local server so test elsewhere if you are seeing this.我从来没有设法完全调试它,但它似乎是本地服务器的问题,所以如果您看到它,请在其他地方进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM