2

I have a php script to load an API. It's functional; however it takes around 15 seconds to load. Is there a way of making the script run better so it has a faster load time?

Heres the PHP:

<?php
error_reporting(0);
$apikey = "apikey=***************************";
$json = file_get_contents("https://eu.api.battle.net/wow/guild/Vek'Nilash/Renascence?fields=news&locale=en_GB&".$apikey);
$feed = json_decode($json);
$feedcount = 0;
foreach($feed->news as $newsfeed) {
    if ($feedcount >= 8) {
        break;
    }
    echo $newsfeed->character . PHP_EOL;
    $type = $newsfeed->type;
    // $iteminfo = $newsfeed->itemId . PHP_EOL;
    $itemnumber = $newsfeed->itemId;
    if ($type == "itemLoot"){
        echo " has looted:";
        $itemurl = file_get_contents("https://eu.api.battle.net/wow/item/$itemnumber?locale=en_GB&".$apikey);
        $itemname = json_decode($itemurl);
        echo " [" . $itemname->name . "]<br>";
    }
    elseif ($type=="itemPurchase"){
        echo " has purchased:";
        $itemurl = file_get_contents("https://eu.api.battle.net/wow/item/$itemnumber?locale=en_GB".$apikey);
        $itemname = json_decode($itemurl);
        echo " [" . $itemname->name . "]<br>";
    }
    elseif ($type=="playerAchievement"){
        echo " has Achieved:";
        echo " " . $newsfeed->achievement->title . "<br>";
    }
    ++$feedcount;
}
?>
CBroe
  • 91,630
  • 14
  • 92
  • 150
J. Bates
  • 31
  • 6
  • First thing you have to answer is: what is the time relevant step in this? Then you know what to optimize. What immediately springs into mind is the http request you make. How long does that take? Second is: how big is the response you have to work through, maybe it can be filtered in the request so that you have to process _less_ information? – arkascha Jan 16 '17 at 12:58
  • That sounds logical. I'll look into it further. Thank You. – J. Bates Jan 16 '17 at 13:16
  • If you ever played the games on that net, you are surely aware of frequent and unpredictable game lags (`their` servers or network, or `your` network). If you have not profiled the contribution of the `file_get_contents` in the loop, you are shooting in the dark when you ask about optimizing this script. – YvesLeBorg Jan 16 '17 at 13:41

1 Answers1

2

If the transferred data is big, try to enable the gzip compression by adding this on the top of your php code:

ob_start('ob_gzhandler');

If that's not the case, check out these links, thay may be helpful:

PHP file_get_contents very slow when using full url

How to speed up file_get_contents?

Community
  • 1
  • 1
  • 1
    How should compressing the scripts output improve it's performance? The code shows that the output contains only a max of 8 times two lines of text. Hardly any sense in compressing such a small payload. And even f hat does not tat the performance of the script itself. – arkascha Jan 16 '17 at 13:25
  • 1
    The rest of the answer is a) of limited quality, since it is a link only answer and b) a wild guess instead of an answer. – arkascha Jan 16 '17 at 13:26
  • The JSON file that is retrieved is over 2k lines long. Would anyone be able to point me in the right direction so I only retrieve the first few hundred lines of the file? Not asking for a step by step answer as I am trying to learn rather than copy and paste. – J. Bates Jan 16 '17 at 14:01
  • 1
    @J.Bates start by measuring how long the json_decode takes, you would surprised how so very few milliseconds will be used to parse your typical 2K lines json file. Look at your code, you have up to 9 https requests in there, make each one (optimistically) at 1.5 seconds, and you have 13.5 seconds total execution time – YvesLeBorg Jan 16 '17 at 14:05
  • You're a great coder, solved my big problem in one-liner code. Thanks – Muddasir23 Apr 05 '23 at 19:24