Skip to content

Instantly share code, notes, and snippets.

@BlakeStevenson
Created October 27, 2019 19:34
Show Gist options
  • Save BlakeStevenson/cf974f482371aee67edb98c6edac1ac4 to your computer and use it in GitHub Desktop.
Save BlakeStevenson/cf974f482371aee67edb98c6edac1ac4 to your computer and use it in GitHub Desktop.
<?php
function school_roster($id) {
$url = "https://www.myutr.com/api/v1/club/$id/school";
// Initialize a CURL session.
$ch = curl_init();
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
// decode JSON
$apiresult = json_decode($result, true);
// create arrays with player names and ratings stored by user id
foreach($apiresult['roster'] as $player) {
$playerNames[$player['id']] = $player['displayName'];
$ratings[$player['id']] = $player['singlesUtr'];
}
// sort ratings greatest to least
arsort($ratings);
// create new roster with names + ratings sorted properly.
foreach($ratings as $id => $rating) {
$roster[] = array(
"name" => $playerNames[$id],
"rating" => $ratings[$id]
);
}
// add school name to output
$finalresult = array(
"schoolName" => $apiresult['displayName'],
"roster" => $roster,
);
return $finalresult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment