php旋度蒸汽api超慢

php旋度蒸汽api超慢,php,curl,steam-web-api,Php,Curl,Steam Web Api,我正在制作一个记分板,并实现steam API来检索用户的头像。起初我用的是file_get,但速度太慢了!所以有人建议我用卷发 旧方法 $url = 'http://www.com'; $content = file_get_contents($url); $json = json_decode($content, true); 然后,我使用foreach循环从数据中获取我想要的项 foreach($output['response']['players'] as $item) { } 新

我正在制作一个记分板,并实现steam API来检索用户的头像。起初我用的是file_get,但速度太慢了!所以有人建议我用卷发

旧方法

$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方法中得到了几乎相同的结果,但速度要快一点。但是速度还是非常慢,有没有办法提高速度呢?我可以加载表格,然后加载可用的化身吗

记分牌

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

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

请注意,steam API一次接受100个ID,因此friendslist被分成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);
$totalfriends=count($friends);
$chunkedfriends=array\u chunk($friends,100);
$chunks=ceil($totalfriends/100);
如果(ob_get_length()>0){
ob_end_flush();
ob_implicit_flush();}
对于($i=0;$i<$chunks;$i++){
$url=”https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0001/?key=“$streamkey.”&streamids=“.introde(',',$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\u exec($ch);
卷曲关闭($ch);
$json_decoded=json_decode($urlresult);
如果(ob_get_length()>0){
ob_end_flush();
ob_implicit_flush();}
对于($x=0;$x<$friendscountchunk;$x++){
?>

考虑使用循环,因为那些可以加快速度。如果你正在谈论加载时间(直到页面加载和显示的时间)慢,考虑使用这样的输出缓冲:

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

请注意,steam API一次接受100个ID,因此friendslist被分成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);
$totalfriends=count($friends);
$chunkedfriends=array\u chunk($friends,100);
$chunks=ceil($totalfriends/100);
如果(ob_get_length()>0){
ob_end_flush();
ob_implicit_flush();}
对于($i=0;$i<$chunks;$i++){
$url=”https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0001/?key=“$streamkey.”&streamids=“.introde(',',$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\u exec($ch);
卷曲关闭($ch);
$json_decoded=json_decode($urlresult);
如果(ob_get_length()>0){
ob_end_flush();
ob_implicit_flush();}
对于($x=0;$x<$friendscountchunk;$x++){
?>

  • 您是否在python代码之外测试了curl的速度?可能是网络…或者您正在从中获取的站点…是的,它似乎是steam api,每个请求都会花费很长时间。是否存在这样的情况?您是否在python代码之外测试了curl的速度?可能是网络…或者您正在从中获取的站点m、 …是的,这似乎是steam api,每一个请求都要花费很长时间。这有什么原因吗?