Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active June 24, 2021 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/a89557d14363ec3afb83a3011515ed07 to your computer and use it in GitHub Desktop.
Save parzibyte/a89557d14363ec3afb83a3011515ed07 to your computer and use it in GitHub Desktop.
<?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)
. "!"
);
}
}
@mauro199304
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment