【问题标题】:php curl steam-api super slowphp curl steam-api 超级慢
【发布时间】:2015-09-23 23:10:48
【问题描述】:

我正在制作记分牌并实现 Steam API 来为用户检索头像。一开始我用的是file_get,但是太慢了!所以有人建议我使用 curl。

老方法

$url = 'http://www.com';
$content = file_get_contents($url);
$json = json_decode($content, true);

然后我使用 foreach 循环从数据中获取我想要的项目。

foreach($output['response']['players'] as $item) {
}

新的 curl 代码,

$url = 'www.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo $output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output, true);

我从 json 方法中得到了几乎相同的结果,但它要快一点。但是还是很慢,有什么办法可以提高这个速度吗?我可以加载表格,然后在头像可用时加载它们吗?

记分牌 http://fyre.site.nfoservers.com/index.php

【问题讨论】:

  • 你在python代码之外测试过curl的速度吗?可能是网络....或您从中获取的站点....
  • 是的,它似乎是 Steam api,每次请求都需要永远。反正有这个吗?

标签: php curl steam-web-api


【解决方案1】:

考虑使用 for 循环,因为这样可以加快速度。如果您正在谈论加载时间(页面加载和显示之前的时间)很慢,请考虑使用如下输出缓冲:

取消设置不再需要的数组或值。

请注意,Steam API 一次接受 100 个 ID,因此好友列表被分成 100 个块。

一旦完成就会推送信息,网站不会等到完成。试试吧,我猜。

$totalfriends = count($friends);
$chunkedfriends = array_chunk($friends, 100);
$chunks = ceil($totalfriends / 100);
if(ob_get_length() > 0) {
ob_end_flush();
ob_implicit_flush();}
for($i=0; $i < $chunks; $i++){
$url = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0001/?key=". $steamkey . "&steamids=". implode(',', $chunkedfriends[$i]) . "";
$friendscountchunk = count($chunkedfriends[$i]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_PIPEWAIT, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$urlresult=curl_exec($ch);
curl_close($ch);
$json_decoded = json_decode($urlresult);
if(ob_get_length() > 0) {
ob_end_flush();
ob_implicit_flush();}
for($x=0; $x < $friendscountchunk; $x++){
  ?>
   <li class="friendsli"><a href="steamuser.php?id=<?=$json_decoded->response->players->player[$x]->steamid?>">
  <img src=' <?=$json_decoded->response->players->player[$x]->avatar?>'/><p class="friendname"> <?=$json_decoded->response->players->player[$x]->personaname?> </p>
</a></li> <?php
}}
 unset($friends); unset($player); unset($json_decoded);

我不认为这是最好的脚本或方法,但它肯定会有所帮助。 您无法加速外部 API,但您可以改进和调整您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2013-01-01
    • 2017-03-27
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多