YOURLS PHP script

I set up a YOURLS instance on gho.st but I wanted users to be able to simply input a long url on the main page and get returned a short url. The script takes a post input and using yourls-api.php it phases the xml output into a php array for display to the users. Enjoy!

html form

<h2>URL Shortening Service</h2>
<form action="shorturl.php" method="post" name="shorturl" id="shorturl">
Long URL:<br />
<input name="shorturl" type="text" id="from" value="http://"/>
<br />
<input name="Submit" type="submit"  value="Shorten URL"/>
</form>

php code for shorturl.php

<?php

// Inputs
$shorturl = $_POST["shorturl"];
$username = "username";
$password = "password";
$siteurl = "http://yourdomain.tld";

// Phases url
$yourlsinput = "".$siteurl."/yourls-api.php?action=shorturl&url=".$shorturl."&username=".$username."&password=".$password."" ;

// Open phased url
$fh = fopen($yourlsinput, 'r') or die("can't open file");
//$yourlsoutput = fread($fh,filesize($yourlsinput));
$yourlsoutput = fread($fh,1024);
fclose($fh);

// Converts xml to php array
$xml = simplexml_load_string($yourlsoutput);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

// Output status
echo "
<html><head><title>Yourls</title></head>" ;
echo "<body><p><b>Status:</b> ".$array['status']."<br>";
echo "<b>Message:</b> ".$array['message']."<br>";
echo "<b>Short url:</b> <a href='".$array['shorturl']."'>".$array['shorturl']."</a></p>";
echo "Return to <a href='/'>home page</a> ? </body></html>";

?>

 

 

7 comments on “YOURLS PHP script
  1. Nate says:

    That is exactly what I have been looking for! Now all I need to do is work out posting to Twitter with PHO and the 1.1 API and I have a fully working, automatic link poster ^_^ Thank you!

  2. Paul says:

    Hi,
    Nice work!!
    I wonder if anybody knows how to add a user column to the short_url table and populate my user session variable while a user creates a short url?

    Thanks

  3. Jordan Fry says:

    Hi,
    this is great, but is there a way to make it use a supplied suffix instead on the numbers?

  4. Daye says:

    Thank you very much. This is a really nice one.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

EnglishFrenchGermanItalianPortugueseRussianSpanish