Created
January 16, 2019 23:59
-
-
Save gustavonovaes/6a7d96f40afb4e6ebf4389601b6fe6db to your computer and use it in GitHub Desktop.
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 | |
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