Last active
June 24, 2021 23:13
-
-
Save parzibyte/a89557d14363ec3afb83a3011515ed07 to your computer and use it in GitHub Desktop.
Use adf.ly API with PHP | https://parzibyte.me/blog/2019/10/30/php-consumir-api-adfly/
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 | |
# Ayudante para saber si cadena comienza con otra cadena | |
function comienzaCon($pajar, $aguja) | |
{ | |
return $aguja === '' | |
|| strrpos($pajar, $aguja, -strlen($pajar)) !== false; | |
} | |
/** | |
* Acortar enlaces usando API de adf.ly | |
* @author parzibyte | |
* @see https://parzibyte.me/blog | |
* @param string $enlace | |
* @return bool|string | |
* @throws Exception | |
*/ | |
function acortarAdfLy($enlace) | |
{ | |
# Si no los tienes, únete en https://join-adf.ly/12358221 | |
$key = "tu_llave"; # Llave pública de API | |
$uid = "tu_id"; # Id de usuario | |
$datos = [ | |
'key' => $key, | |
"uid" => $uid, | |
'advert_type' => 'interstitial', | |
'domain' => 'q.gs', | |
"url" => $enlace, | |
]; | |
$resultado = @file_get_contents('http://api.adf.ly/api.php?' . http_build_query($datos)); | |
if ($resultado === false) { | |
throw new Exception( | |
"Error al realizar la conexión para acortar $enlace con adf.ly!" | |
); | |
} | |
/* | |
* Si no respondieron con un JSON entonces | |
* respondieron con la URL, y todas las cosas | |
* están bien | |
* */ | |
$respuestaDecodificada = json_decode($resultado); | |
if ($respuestaDecodificada === null) { | |
if (comienzaCon($resultado, "ERROR:")) { | |
throw new Exception("El enlace $enlace no es válido: $resultado"); | |
} | |
$acortado = $resultado; | |
if (preg_match('/^http:\/\/q\.gs\/\w+$/', $acortado) !== 1) { | |
throw new Exception("Enlace inesperado al acortar con adf.ly: " . $acortado); | |
} | |
return $acortado; | |
} else { | |
throw new Exception( | |
"Error en la respuesta del servidor al acortar con adf.ly" | |
. json_encode($respuestaDecodificada->errors, true) | |
. "!" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
brother, what parameter to send to update a web site that is already shortened with adf.ly, I mean I want to change the url already shortened