Created
July 19, 2016 17:02
-
-
Save garethtdavies/8ee04a3826c3dcb140e5bd1ac2f90da6 to your computer and use it in GitHub Desktop.
Extract all individual medal winners at Mixed Relay World Championships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
| Finds individual medal winners at Team Champs | |
| Add your API key on line 10 | |
*/ | |
// Set some basic constants here | |
define("APIURL", "https://api.triathlon.org/v1/"); | |
define("APIKEY", "YOUR_API_KEY_HERE"); | |
// This function takes the cURL responses and parses the output | |
function getResponse($url) | |
{ | |
$response = doCurl(APIURL . $url); | |
// Should do some error checking here to ensure you are getting a 200 | |
$resp = json_decode($response['resp'], true); | |
return $resp; | |
} | |
// This function performs the cURL request for a given URL | |
function doCurl($url) | |
{ | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $url, | |
CURLOPT_SSL_VERIFYPEER => FALSE, | |
CURLOPT_HTTPHEADER => array( | |
"apikey: " . APIKEY | |
) | |
)); | |
$resp = curl_exec($curl); | |
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
//Build up a return array that includes the cURL response header | |
$return = array( | |
'code' => $code, | |
'resp' => $resp | |
); | |
curl_close($curl); | |
return $return; | |
} | |
// Let's output all the formatted data directly | |
echo "Event Name | Event Date | Position | Country Name | Nat | Athlete ID | Athlete Title | Athlete Gender \r\n"; | |
echo "----------------------------------------------------------------------------------------------------- \r\n"; | |
// Start the analysis | |
// Get all Mixed Relay events from the Search API | |
$events = getResponse("search/events?query=Mixed%20Relay%20World%20Championships"); | |
// Loop through each event | |
foreach ($events['data'] as $event) { | |
// Extract the prog_id | |
$programs = getResponse("events/{$event['event_id']}/programs?prog_filter=4xMixed%20Relay"); | |
foreach ($programs['data'] as $program) { | |
if($program['results'] === false) continue; // Sanity check in case future events are in here | |
// Get the results of the program | |
$results = getResponse("events/{$event['event_id']}/programs/{$program['prog_id']}/results?position=1|2|3"); | |
foreach($results['data']['results'] as $result) { | |
// Loop through each team member who was part of a medal winning relay team | |
foreach($result['team_members'] as $member) { | |
// These are all the athletes who have a medal | |
echo "{$event['event_title']} | {$event['event_date']} | {$result['position']} | {$result['team_country_name']} | {$result['team_noc']} | {$member['athlete_id']} | {$member['athlete_title']} | {$member['athlete_gender']} \r\n"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment