Multiple curl queries in single function

Hello all,
i have a function that pulls api data from a remote server, what i need to do is pull 3 sets of data so that i can display portions of each call on a page. the current code is working fine its just im sure that there is a much better way of doing this as my way is really silly …

[php]function stats($pID){
$ch0 = curl_init();
$ch1 = curl_init();
$ch2 = curl_init();
// Player Stats
$apiuri0 = ‘https://api.worldofwarships.com/wows/account/info/?application_id=a3a64dfac090e84c33c25d816e4d2ccd&account_id=’.$pID.’&language=en’;
// Achievements
$apiuri1 = ‘https://api.worldofwarships.com/wows/account/achievements/?application_id=a3a64dfac090e84c33c25d816e4d2ccd&account_id=’.$pID.’&language=en’;
// Ranked Stats
$apiuri2 = ‘https://api.worldofwarships.com/wows/seasons/accountinfo/?application_id=a3a64dfac090e84c33c25d816e4d2ccd&account_id=’.$pID.’&language=en’;
curl_setopt($ch0, CURLOPT_URL, $apiuri0);
curl_setopt($ch1, CURLOPT_URL, $apiuri1);
curl_setopt($ch2, CURLOPT_URL, $apiuri2);
curl_setopt($ch0, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$output0 = json_decode(curl_exec($ch0), true);
$output1 = json_decode(curl_exec($ch1), true);
$output2 = json_decode(curl_exec($ch2), true);
curl_close($ch0);
curl_close($ch1);
curl_close($ch2);
foreach ($output0[‘data’] AS $key0 => $value0) {
echo $nick.’
’;
echo ‘


’;
echo ‘

Raw output:

’;
echo ‘
’;
print_r ($value0);
echo ‘
’;
}
foreach ($output1[‘data’] AS $key1 => $value1) {
echo ‘
’;
echo ‘

Raw output:

’;
echo ‘
’;
print_r ($value1);
echo ‘
’;
}
foreach ($output2[‘data’] AS $key2 => $value2) {
echo ‘
’;
echo ‘

Raw output:

’;
echo ‘
’;
print_r ($value2);
echo ‘
’;
}
}
echo stats($pID);[/php]

You could merge the loops, but in terms of the requests themselves, there are three distinct endpoints. Now, you could abstract out the function, and call it in a loop with the differences.

can you provide an example, im a bit lost at to what you are saying… or maybe my explanation wasnt on point?? …

here is the FULL function that i am using … this is being used in a PHPBB extension.
i believe that i am stuck with 1 function thus i was trying to collapse the code a bit to make it easier to read as well as more to best practice…

This function pulls a the wws_id value from the users profile
[php] public function memberlist_view_profile($event)
{
$member = $event[‘member’];
$user_id = (int) $member[‘user_id’];
$this->memberlist->medal_row($user_id);
$profile_fields = $event[‘profile_fields’];
if (isset($profile_fields[‘row’][‘PROFILE_WWS_ID_VALUE’]))
{
$this->process_wws_id($profile_fields[‘row’][‘PROFILE_WWS_ID_VALUE’]);
};
}
[/php]

This function takes that wws_id and turns it into a var that is then used in the uri’s that curl is pulling and then sets up the output
[php]public function process_wws_id($wws_id)
{
$this->template->assign_vars(array(
‘S_WWS_HAS_PROFILE’ => true,
‘WWS_ID’ => $wws_id,
));

	$ch0 = curl_init();
	$ch1 = curl_init();
	$ch2 = curl_init();
	// Player Stats
	$apiuri0 = 'https://api.worldofwarships.com/wows/account/info/?application_id=a3a64dfac090e84c33c25d816e4d2ccd&account_id='.$wws_id.'&language=en';
	// Achievements
	$apiuri1 = 'https://api.worldofwarships.com/wows/account/achievements/?application_id=a3a64dfac090e84c33c25d816e4d2ccd&account_id='.$wws_id.'&language=en';
	// Ranked Stats
	$apiuri2 = 'https://api.worldofwarships.com/wows/seasons/accountinfo/?application_id=a3a64dfac090e84c33c25d816e4d2ccd&account_id='.$wws_id.'&language=en';
	curl_setopt($ch0, CURLOPT_URL, $apiuri0);
	curl_setopt($ch1, CURLOPT_URL, $apiuri1);
	curl_setopt($ch2, CURLOPT_URL, $apiuri2);
	curl_setopt($ch0, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
	$pstats = json_decode(curl_exec($ch0), true);
	$achievs = json_decode(curl_exec($ch1), true);
	$ranked = json_decode(curl_exec($ch2), true);
	curl_close($ch0);
	curl_close($ch1);
	curl_close($ch2);
		foreach ($pstats['data'] AS $key => $value) {
			$this->template->assign_block_vars(wowsstats, array(
			'WWS_NICKNAME'		=> $value['nickname'],
			'WWS_TBATTLES'		=> $value['statistics']['pvp']['battles'],
			'WWS_WINS'			=> $value['statistics']['pvp']['wins'],
			'WWS_LOSS'			=> $value['statistics']['pvp']['losses'],
			'WWS_DRAWS'			=> $value['statistics']['pvp']['draws'],
			'WWS_KILLS'			=> $value['statistics']['pvp']['frags'],
			'WWS_RATIO'			=> number_format($value['statistics']['pvp']['wins']/$value['statistics']['pvp']['battles']*100),
			'WWS_RAWDATA'		=> print_r ($value, true),				
		));
	}
		foreach ($achievs['data'] AS $key => $value) {
			$this->template->assign_block_vars(wowsachievs, array(
			'WWSA_DOUBLE_KILL'			=> $value['battle']['DOUBLE_KILL'],
			'WWSA_LIQUIDATOR'			=> $value['battle']['LIQUIDATOR'],
			'WWSA_WARRIOR'				=> $value['battle']['WARRIOR'],
			'WWSA_DETONATED'			=> $value['battle']['DETONATED'],
			'WWSA_WITHERING'			=> $value['battle']['WITHERING'],
			'WWSA_ARSONIST'				=> $value['battle']['ARSONIST'],
			'WWSA_CBT_PARTICIPANT'		=> $value['battle']['CBT_PARTICIPANT'],
			'WWSA_RAWDATA'				=> print_r ($value, true),				
		));
	}
		foreach ($ranked['data'] AS $key => $value) {
			$this->template->assign_block_vars(wowsranked, array(
			'WWSR_MAXRANK1'			=> $value['seasons']['1']['rank_info']['max_rank'],
			'WWSR_STARTRANK1'		=> $value['seasons']['1']['rank_info']['start_rank'],
			'WWSR_RANK1'			=> $value['seasons']['1']['rank_info']['rank'],
			'WWSR_MAXRANK2'			=> $value['seasons']['2']['rank_info']['max_rank'],
			'WWSR_STARTRANK2'		=> $value['seasons']['2']['rank_info']['start_rank'],
			'WWSR_RANK2'			=> $value['seasons']['2']['rank_info']['rank'],
			'WWSR_MAXRANK3'			=> $value['seasons']['3']['rank_info']['max_rank'],
			'WWSR_STARTRANK3'		=> $value['seasons']['3']['rank_info']['start_rank'],
			'WWSR_RANK3'			=> $value['seasons']['3']['rank_info']['rank'],
			'WWSR_MAXRANK4'			=> $value['seasons']['4']['rank_info']['max_rank'],
			'WWSR_STARTRANK4'		=> $value['seasons']['4']['rank_info']['start_rank'],
			'WWSR_RANK4'			=> $value['seasons']['4']['rank_info']['rank'],
			'WWSR_MAXRANK5'			=> $value['seasons']['5']['rank_info']['max_rank'],
			'WWSR_STARTRANK5'		=> $value['seasons']['5']['rank_info']['start_rank'],
			'WWSR_RANK5'			=> $value['seasons']['5']['rank_info']['rank'],
			'WWSR_MAXRANK6'			=> $value['seasons']['6']['rank_info']['max_rank'],
			'WWSR_STARTRANK6'		=> $value['seasons']['6']['rank_info']['start_rank'],
			'WWSR_RANK6'			=> $value['seasons']['6']['rank_info']['rank'],
			'WWSR_RAWDATA'			=> print_r ($value, true),				
		));
	}
}[/php]

well i have fixed this issue by creating seperate functions for each api call … this makes it alot easier.

Sponsor our Newsletter | Privacy Policy | Terms of Service