Created
October 31, 2019 01:22
-
-
Save parzibyte/023a623ca12bcdcb8b6d9661b9ffa02c to your computer and use it in GitHub Desktop.
Short a shink.me URL with PHP | https://parzibyte.me/blog/2018/08/14/api-de-shinkme-en-php/
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 | |
/** | |
* Acortar con shink.me | |
* @author parzibyte.me/blog | |
*/ | |
function shinkme($enlace) | |
{ | |
$clave = "TU_CLAVE_AQUÍ"; | |
$id = "TU_ID_AQUÍ"; | |
$raw = file_get_contents( | |
"https://shon.xyz/api/0/id/" | |
. urlencode($id) | |
. "/auth_token/" | |
. urlencode($clave) | |
. "?s=" . urlencode($enlace)); | |
if (false === $raw) { | |
throw new \Exception("Error obteniendo JSON de shink.me"); | |
} | |
$respuesta = json_decode($raw); | |
# Comprobar si no hay errores | |
if (isset($respuesta->error) && $respuesta->error === 0) { | |
# Ahora este es el acortado, pero vamos a ver si coincide con una expresión regular | |
$acortado = "http://shink.me/" . $respuesta->hash; | |
if (preg_match('/^http:\/\/shink\.me\/\w+$/', $acortado) !== 1)# En caso de que no coincida | |
throw new Exception("Enlace inesperado al acortar con shink.me: " . $acortado); | |
return $acortado; #En caso de que sí regresamos el acortado | |
} else { | |
throw new Exception("Error al acortar $enlace con shink.me. #Error: " . $respuesta->error); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment