优化PHP代码和存储JSON响应以更快地解析它


Optimizing PHP Code and Storing JSON response to parse it faster?

所以我想弄清楚为什么这个PHP代码需要太长时间才能运行输出结果。

例如

这是我的apitest.php这是我的PHP代码

<?php
function getRankedMatchHistory($summonerId,$serverName,$apiKey){
$k
$d;
$a;
$timeElapsed;
$gameType;
$championName;
$result;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://".$serverName.".api.pvp.net/api/lol/".$serverName."/v2.2/matchhistory/".$summonerId."?api_key=".$apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$matchHistory = json_decode($response,true); // Is the Whole JSON Response saved at $matchHistory Now locally as a variable or is it requested everytime $matchHistory is invoked ?
for ($i = 9; $i >= 0; $i--){
    $farm1 = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["minionsKilled"];
    $farm2 = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["neutralMinionsKilled"];
    $farm3 = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["neutralminionsKilledTeamJungle"];
    $farm4 = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["neutralminionsKilledEnemyJungle"];
    $elapsedTime = $matchHistory["matches"][$i]["matchDuration"];
    settype($elapsedTime, "integer");
    $elapsedTime = floor($elapsedTime / 60);
    $k = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["kills"];
    $d = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["deaths"];
    $a = $matchHistory["matches"][$i]["participants"]["0"]["stats"]["assists"];
    $championIdTmp = $matchHistory["matches"][$i]["participants"]["0"]["championId"];
    $championName =  call_user_func('getChampionName', $championIdTmp); // calls another function to resolve championId into championName
    $gameType = preg_replace('/[^A-Za-z0-9'-]/', ' ', $matchHistory["matches"][$i]["queueType"]);
    $result = (($matchHistory["matches"][$i]["participants"]["0"]["stats"]["winner"]) == "true") ? "Victory" : "Defeat";
    echo "<tr>"."<td>".$gameType."</td>"."<td>".$result."</td>"."<td>".$championName."</td>"."<td>".$k."/".$d."/".$a."</td>"."<td>".($farm1+$farm2+$farm3+$farm4)." in ". $elapsedTime. " minutes". "</td>"."</tr>";
    }
}
?>

我想知道的是如何使页面输出更快,因为它需要周围10~15秒输出结果,使浏览器认为该网站已死,如500内部错误或类似的东西。

这里有一个简单的例子来说明它需要多长时间:

正如你可能已经注意到的,是的,我正在使用Riot API,它将响应作为JSON编码类型发送。

下面是该函数处理的响应示例:

我想到的是在CURL函数开始时创建一个名为temp.php的临时文件,并在那里保存整个响应,然后从那里读取变量,这样我就可以加快进程,在读取变量后删除创建的temp.php,从而释放磁盘空间。并提高速度

但是我不知道如何在PHP中这样做。

顺便说一下,我想告诉你,我今天才开始使用PHP,所以如果可能的话,我希望有一些答案的解释。

感谢您的宝贵时间。

尝试这样的基准测试:

// start the timer
$start_curl = microtime(true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://".$serverName.".api.pvp.net/api/lol/".$serverName."/v2.2/matchhistory/".$summonerId."?api_key=".$apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// debugging
curl_setopt($ch, CURLOPT_VERBOSE, true); 
// start another timer
$start = microtime(true);
$response = curl_exec($ch);
echo 'curl_exec() in: '.(microtime(true) - $start).' seconds<br><br>';
// start another timer
$start = microtime(true);
curl_close($ch);
echo 'curl_close() in: '.(microtime(true) - $start).' seconds<br><br>';
// how long did the entire CURL take?
echo 'CURLed in: '.(microtime(true) - $start_curl).' seconds<br><br>';