Create Digg URLs Using PHP

By  on  

Digg recently came out with a sweet new feature that allows users to create Tiny Digg URLs which show a Digg banner at the top allowing easy access to vote for the article from the page. While I love visiting Digg every once in a while, I'd rather grab the URL remotely. Here's how to do so using PHP.

The PHP

/* function that grabs the response from digg */
function get_digg_url($url,$app_key)
{
	$return_xml = file_get_contents('http://services.digg.com/url/short/create?type=xml&appkey='.urlencode($app_key).'&url='.urlencode($url));
	$digg_url = get_match('/short_url="(.*)"/isU',$return_xml);
	return $digg_url;
}

/* function that runs a regex to scrub for the url */
function get_match($regex,$content)
{
	preg_match($regex,$content,$matches);
	return $matches[1];
}

/* important! set a fake user agent */
ini_set('user_agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');

/* url i want the digg URL for, and my app key which is a URL */
$url = 'https://davidwalsh.name/penetrated-diggnation';
$app = 'https://davidwalsh.name';

/* get the digg URL! */
$digg_url = get_digg_url($url,$app);  //returns:  http://digg.com/u1DOk

Very quick and simple. You could also use PHP's cURL library if you wanted.

The XML Response

<?xml version="1.0" encoding="UTF-8"?>
<shorturls count="1" offset="0" timestamp="1238884894" total="1">
	<shorturl link="https://davidwalsh.name/penetrated-diggnation" short_url="http://digg.com/u1DOk" view_count="0"/>
</shorturls>

XML is a beautiful thing, isn't it? You may also request a JSON response.

Like this Digg article? I suppose you could Digg it! Or you can check out the time I was featured on DiggNation!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    Fx.Rotate:  Animated Element Rotation with MooTools

    I was recently perusing the MooTools Forge and I saw a neat little plugin that allows for static element rotation: Fx.Rotate. Fx.Rotate is an extension of MooTools' native Fx class and rotates the element via CSS within each A-grade browser it...

  • By
    Fade Images with MooTools LazyLoad

    I recently received an email from a MooTools developer asking a great question about my LazyLoad class: "I'm using your LazyLoad MooTools plugin (which is great, by the way). I have been trying to figure out how to modify it so that once an image scrolls into...

Discussion

  1. Note: My attempts with cURL were unsuccessful but that was likely a hosting provider issue.

  2. Excellent David.

    However, maybe this would be better suited to an object to bring those depended functions together. Additionally, it maybe best to avoid all those ini_set shenanigans and embrace the XML rather that RegEx’n it.

    #Object
    class ShortDiggURL
    {
        protected $sAppKey;
    
        public function __construct($sAppKey)
        {
            $this->sAppKey = $sAppKey;
        }
    
        public function getShortURLFor($sLongURL)
        {
            $rContext = stream_context_create(
                array(
                    'http' => array(
                        'timeout'       =>  15,
                        'user_agent'    => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'
                    )
                )
            );
            $oResponse = @new SimpleXMLElement(
                file_get_contents(
                    sprintf(
                        'http://services.digg.com/url/short/create?type=xml&appkey=%s&url=%s',
                        urlencode($this->sAppKey),
                        urlencode($sLongURL)
                    ),
                    null,
                    $rContext
                )
            );
            if($oResponse instanceof SimpleXMLElement)
            {
                return $oResponse->shorturl['short_url'];
            }
            return false;
        }
    }
    
    #Usage
    $oShortDiggURL = new ShortDiggURL('http://davidwalsh.name');
    echo $oShortDiggURL->getShortURLFor('http://davidwalsh.name/penetrated-diggnation');
    
  3. @Anthony Sterling: I like the regex because it prevents the overhead of bringing in XML parsing. In all honesty, not using a XML reader is probably safer.

  4. Thanks, this is nice… I wonder how long tinyurl will last now that digg competes with them?

  5. Very nice! For some reason I wasn’t getting any return HTTP request – and the reason was because I didn’t set the User Agent! Thanks for the tip!

  6. Keith

    How would I get this to link from an image?

    I have my own image, how would I get that image to link to the Auto-generated DIGG Url?

    Thanks,
    Keith.

  7. This script does not work anymore, can you please tell me how to post to digg using PHP. Thanks.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!