Skip to content

Instantly share code, notes, and snippets.

@gustavonovaes
Created January 16, 2019 23:59
Show Gist options
  • Save gustavonovaes/6a7d96f40afb4e6ebf4389601b6fe6db to your computer and use it in GitHub Desktop.
Save gustavonovaes/6a7d96f40afb4e6ebf4389601b6fe6db to your computer and use it in GitHub Desktop.
<?php
const LOGIN = 'o_1233211233';
const API_KEY = 'R_2131233211234232111112223232323232';
function httpGet(string $url) : string
{
if (false === $result = file_get_contents($url)) {
throw new RuntimeException('Request failed');
}
return $result;
}
function shortURL(string $urlTarget, string $login, string $apiKey) : string
{
$uri = implode('', [
'http://api.bit.ly/v3/shorten',
'?login=' . $login,
'&apiKey=' . $apiKey,
'&uri=' . urlencode($urlTarget)
]);
[
'data' => ['url' => $shortUrl],
'status_txt' => $status
] = json_decode(httpGet($uri), true);
if ($status !== "OK") {
throw new RuntimeException($status);
}
return $shortUrl;
}
$result = shortURL('https://github.com/gustavonovaes', LOGIN, API_KEY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment