Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active October 31, 2019 05:29
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/ebffb687e84202ba429967f8e2cdf06c to your computer and use it in GitHub Desktop.
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/
<?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