简体   繁体   中英

PHP cURL requests being slow

im trying to grab the contents from a URL(which is a json) that changes for each iteration of my loop. The problem with my method of doing things is that it is very slow and if I do about 120 iterations it takes over 40sec.

Here is my code:

$GetFriendListUrl = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=mykey&steamid=".$other_steamid."&relationship=friend";
$GET_GetFriendListUrl= file_get_contents($GetFriendListUrl);

$raw_ids = json_decode($GET_GetFriendListUrl , TRUE);
$count = count($raw_ids['friendslist']['friends']);

$ci = curl_init();
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

for ($x = 0; $x <= $count; $x++) {
    $friendslist = $raw_ids['friendslist']['friends'][$x]['steamid'];

    curl_setopt($ci, CURLOPT_URL, "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=mykey&steamids=".$friendslist);
    $cont = curl_exec($ci);
    $contFull = json_decode($cont, true);

    $steamname = $contFull['response']['players'][0]['personaname'];
    $steamprofileurl = $contFull['response']['players'][0]['profileurl'];
    $friendimage = $contFull['response']['players'][0]['avatar'];
 
    $friendimageData = base64_encode(file_get_contents($friendimage));

    echo '<img class="other_friendsteamimage" src="data:image/jpeg;base64,'.$friendimageData.'">';
    echo "<a class='other_friendlabel' href='$steamprofileurl'>$steamname</a>";
    echo "<br>";
}
curl_close($ci);

I cannot be sure of the format of the data returned by the api and I have no means of testing the following but in line with the comment I made and based upon the documentation it would appear that sending few requests but with each request dealing with 100 steamIDs you should save considerable amount of time.

/* get the intial data */
$url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=mykey&steamid=".$other_steamid."&relationship=friend";
$data= file_get_contents( $url );


$json = json_decode( $data );
$ids=array();

/* just grab the IDs and add to array - correct format to access records??? */
foreach( $json->friendslist->friends as $obj ){
    $ids[]=$obj->steamid;
}

/* split the IDs into chunks of 100 */
$chunks=array_chunk( $ids, 100 );


/* send a request per chunk of 100 */
foreach( $chunks as $chunk ){

    $url=sprintf('https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=mykey&steamids=%s',implode(',',$chunk));
    $curl = curl_init( $url );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $res=curl_exec( $curl );
    if( $res ){
        $data=json_decode($res,true);
        /* do stuff .... */
    }
    curl_close($curl);
}

echo 'Finito';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM