Skip to content

Instantly share code, notes, and snippets.

@haroldiedema
Last active October 17, 2016 17:03
Show Gist options
  • Save haroldiedema/7cb687109eb96402d131c16530a34999 to your computer and use it in GitHub Desktop.
Save haroldiedema/7cb687109eb96402d131c16530a34999 to your computer and use it in GitHub Desktop.
<?php
// LICENSE : http://www.wtfpl.net/txt/copying/
/**
* Returns a raw request URI ready to be sent to the battle.net API.
*
* @param string $url
* @param array $parameters
* @return string
*/
function getRequestURL($url, array $parameters = [])
{
$final_url = 'https://eu.api.battle.net/';
// Apply a rawlurlencode-pass to every chunk in the request URI.
// This piece will handle any special characters in your request
// and make sure you won't get any 404's.
$url = implode('/', array_map(function ($chunk) {
return rawurlencode($chunk);
}, explode('/', $url)));
// Apply rawlurlencode-pass to every value in the parameters.
// Not that this is needed, but it's consistent with the behaviour
// of the URL itself.
foreach ($parameters as $key => $value) {
$parameters[$key] = rawurlencode($value);
}
// Apply the parameters (if any)
if (count($parameters) > 0) {
$url .= '?' . http_build_query($parameters);
}
// Return the final URL. Trim leading trailing slashes and add one forcefully to ensure there is only one / between
// the Base URL and our request URL.
return rtrim($final_url, '/') . '/' . ltrim($url, '/');
}
// Fetch a character with a special character in the name... No biggy :)
$url = getRequestURL('/wow/characters/Stormscale/Gínkeltjes', [
'fields' => 'items',
'locale' => 'en_US',
'apikey' => 'API_KEY'
]);
$data = json_decode(file_get_contents($url));
// $url = https://eu.api.battle.net/wow/characters/Stormscale/G%C3%ADnkeltjes?fields=items&locale=en_US&apikey=API_KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment