如何优化我的PHP脚本,加载API加载更快

时间:2017-01-16 12:52:10

标签: php performance

我有一个PHP脚本来加载API。它的功能;但是加载大约需要15秒。有没有办法让脚本运行得更好,因此加载时间更快?

继承人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;
}
?>

1 个答案:

答案 0 :(得分:1)

如果传输的数据很大,请尝试通过在php代码的顶部添加gzip压缩来启用gzip压缩:

ob_start('ob_gzhandler');

如果情况并非如此,请查看这些链接,这可能会有所帮助:

PHP file_get_contents very slow when using full url

How to speed up file_get_contents?