0

so I'm trying to figure out why does this PHP code takes too long to run to output the results.

for example this is my apitest.php and here is my PHP Code

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

What I'd like to know is how to make the page output faster as it takes around 10~15 seconds to output the results which makes the browser thinks the website is dead like a 500 Internal error or something like it .

Here is a simple demonstration of how long it can take : Here

As you might have noticed , yes I'm using Riot API which is sending the response as a JSON encoded type.

Here is an example of the response that this function handles : Here

What I thought of was creating a temporarily file called temp.php at the start of the CURL function and saving the whole response there and then reading the variables from there so i can speed up the process and after reading the variables it deletes the temp.php that was created thus freeing up disk space. and increasing the speed.

But I have no idea how to do that in PHP Only.

By the way I'd like to tell you that i just started using PHP today so I'd prefer some explanation with the answers if possible .

Thanks for your precious time.

1 Answers1

0

Try benchmarking like this:

// 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>';
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • CURLed in: 8.4606051445007 seconds. check the demonstration website , I updated it. for only the getRankedMatchHistory function and since webpage loads for 11 seconds avg and this functions finish after 8.5 seconds , i guess it's the problem right. – TheOnlyOne Aug 11 '15 at 21:10
  • @TheOnlyOne bingo! Now you know how to properly benchmark your app :-) – MonkeyZeus Aug 11 '15 at 22:48
  • Yes but now I still don't know how to improve the load time. – TheOnlyOne Aug 12 '15 at 08:59
  • @TheOnlyOne The URL which you are CURLing to is returning a 429 error. Have you looked into this? It is possible that the CURL is having issues with a 429 status code. I would look into debugging the CURL request with **[CURLOPT_VERBOSE](http://php.net/manual/en/function.curl-setopt.php)** – MonkeyZeus Aug 12 '15 at 14:16
  • Naah it just an API Rate limit exceeded error , here you go , you can use this link https://eune.api.pvp.net/api/lol/eune/v2.2/matchhistory/29993137?api_key=13db9f40-3d10-4be6-81a8-6a9ed828136c – TheOnlyOne Aug 12 '15 at 14:33
  • @TheOnlyOne I see, please see the updates in my answer. – MonkeyZeus Aug 12 '15 at 15:04
  • @TheOnlyOne If PHP is not sufficient to solve the problem then you might need to install Wireshark on your server and debug the request at the network level. I would recommend creating a local web server on your machine and see if CURL behaves similarly. This could be a DNS related issue. Are you able to CURL to the service via IP address? – MonkeyZeus Aug 12 '15 at 15:12
  • I have been getting a 500 Internal error because my php request took more than 20 second , another page using the same curl method to get a response from a different API . So is there is a way to fix the 500 Internal error timeout ( fail safe time out i guess ) ? – TheOnlyOne Aug 12 '15 at 20:29
  • @TheOnlyOne You can try the methods listed [here](http://stackoverflow.com/q/5164930/2191572) but if your server's security is tight then this might be off-limits for you. If you have 100% access to the server and it's OS then this should be feasible. – MonkeyZeus Aug 12 '15 at 20:32