Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

css - 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);
question from:https://stackoverflow.com/questions/65887422/php-curl-requests-being-slow

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...