Working with YOURLS URL Shortening APIs

In this tutorial, I will show you how to work with YOURLS APIs. Yourls provides a small set of PHP scripts that will allow you to run your own URL shortening service. Here’s how you can setup Yourls to build your custom URL shortening service.

https://yourls.org/#Install

For more specific scenarios you could use its APIs to perform certain tasks.

Checks Whether a Shortened URL Exists

You may need to check if a shortened URL already exists for a given URL. The forllowing function checks whether a shortened url exists and returns the keyword if it exists.

function checkurl($url)
{
$username = '<username>';
$password = '<password>';
$api_url = 'http://<domain_name>/yourls-api.php';
$format="json";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'url'      => $url,
        'action'   => 'geturl',
        'format'   => $format,
        'username' => $username,
        'password' => $password
    ) );
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
//echo $data;
if($obj->{'message'}=="success: found")
{
$keyword=$obj->{'keyword'};
return "success,".$keyword;
}
else
{
return "failed,no keyword";
}
}

Shorten the URL

The following function tries to shorten the url and returns the shorturl if success occurs.

function shortenurl($url)
{
$username = '<username>';
$password = '<password>';
$api_url = 'http://<domain_name>/yourls-api.php';
$format="json";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'url'      => $url,
        'action'   => 'shorturl',
        'keyword'=>'',
        'format'   => $format,
        'username' => $username,
        'password' => $password
    ) );
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
if(strstr($obj->{'status'},"success"))
{
$shorturl=$obj->{'shorturl'};
return "success,".$shorturl;
}
else
{
return "failed,could not shorten";
}
}

Get Expanded URL from Shortened URL

The following function takes a short url and expands it.

function longurl($url) //takes a short url and expands it
{
$username = '<username>';
$password = '<password>';
$api_url = 'http://<domain_name>/yourls-api.php';
$ch = curl_init();
$format="json";
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'shorturl'      => $url,
        'action'   => 'expand',
        'format'   => $format,
        'username' => $username,
        'password' => $password
    ) );
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
$longurl=$obj->{'longurl'};
return $longurl;
}

This content has been helpful to you?

Thanks for contributing!

Yes No