Last active
October 31, 2019 05:29
-
-
Save parzibyte/ebffb687e84202ba429967f8e2cdf06c to your computer and use it in GitHub Desktop.
Use shortzon.com API with PHP to short a link | https://parzibyte.me/blog/2019/10/30/api-shortzon-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 un enlace usando la API de shortzon.com | |
* @author parzibyte | |
* @see https://parzibyte.me/blog | |
* @param string $enlace | |
* @return string | |
* @throws Exception | |
*/ | |
function acortarConShortzon($enlace) | |
{ | |
#Coloca aquí tu token, si no tienes uno, consíguelo en https://shortzon.com/ref/parzibyte | |
$apiToken = "TU_TOKEN"; # Tu api Token | |
$url = sprintf("https://shortzon.com/api?api=%s&url=%s", $apiToken, urlencode($enlace)); | |
$respuesta = @file_get_contents($url); | |
if (!$respuesta) { | |
throw new Exception("Error acortando $enlace al comunicar con la API"); | |
} | |
$datos = json_decode($respuesta); | |
if (!property_exists($datos, "shortenedUrl") || empty($datos->shortenedUrl)) { | |
throw new Exception("No se devolvió un enlace válido: $respuesta"); | |
} | |
$acortado = $datos->shortenedUrl; | |
if (preg_match('/^http[s]:\/\/shrtz\.me\/\w+$/', $acortado) !== 1) { | |
throw new Exception("Enlace inesperado al acortar con shortzon.com el enlace $enlace: " . $acortado); | |
} | |
return $acortado; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment