I am trying to create an Adfly shortener website for my associates to use using PHP. I thought this would be simple, but I am having some trouble getting this to work
$longURL = $_GET['longurl'];
$shortURL = shortAdfly($longURL);
// Print the result
print_r($shortURL);
// Adf.ly shortener
function shortAdfly($ToConvert) {
urlencode($ToConvert);
$apiUrl = 'http://api.adf.ly/api.php?' . http_build_query([
'key' => 'My API Key',
'uid' => 'My User ID',
'advert_type' => 'int',
'domain' => 'adf.ly',
'url' => $ToConvert ]);
$short_url = file_get_contents($apiUrl);
return $short_url;
}
Building URL query strings by hand is like encoding JSON by hand; you just shouldn't do it. Use the tools provided
$apiUrl = 'http://api.adf.ly/api.php?' . http_build_query([
'key' => 'your-api-key',
'uid' => 1234567,
'advert_type' => 'int',
'domain' => 'adf.ly',
'url' => $ToConvert
]);
return file_get_contents($apiUrl);