AddThis Social Bookmark Button

There are two primary methods for interfacing with Xhref. The standard method is the web interface. Links can also be shortened programmatically using the XML API.

If you run into any issues or have any questions about the APIs, let us know.

XML API

The XML API can be accessed by any programming language. The format of the request and response are documented below.

REQUEST

The request is a simple HTML URL. The format of the URL is:

http://xhref.com/cxml?url=HTTP_URL

E.g., http://xhref.com/cxml?url=http://www.google.com

RESPONSE

The response is a basic XML document. The format of the document is:

<?xml version="1.0" encoding="UTF-8"?> <xhrefMsg> <errorCode/> <redirectId>13b</redirectId> <redirectUrl><![CDATA[http://xhref.com/13b]]></redirectUrl> <originUrl><![CDATA[http://www.google.com]]></originUrl> </xhrefMsg>

ERROR CODES

The possible error codes:

  • URL_NOT_SET - If the URL is not set in the request.
  • URL_TOO_LONG - The URL is longer than 1,000 characters
  • URL_INVALID - If URL is not valid.
  • SYSTEM_ERROR - If there is a server side problem with the URL submission.

PHP EXAMPLE

The example implementation below assumes that both the CURL and XML libraries are compiled into PHP.

<?php $response = shorten_url('http://deftlabs.com/2008/06/is-google-doing-something-evil/'); if ($response !== false) { print("redirect id: $response->redirectId\n"); print("redirect url: $response->redirectUrl\n"); print("origin url: $response->originUrl\n"); print("error code: $response->errorCode\n"); } function shorten_url($url) { $xhref_url = 'http://xhref.com/cxml?url=' . urlencode($url); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $xhref_url); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($curl); if (curl_errno($curl) != 0) { curl_close($curl); return false; } curl_close($curl); return new SimpleXMLElement($data); } ?>

PYTHON EXAMPLE

The example implementation below uses standard Python libraries.

from urllib import urlencode, urlopen from elementtree.ElementTree import XML def shorten_url(url): xhref_url = 'http://xhref.com/cxml?' + urlencode({'url':url}) return XML(urlopen(xhref_url).read()) response = shorten_url('http://deftlabs.com/2008/06/is-google-doing-something-evil/') print response.findtext('redirectId') print response.findtext('redirectUrl') print response.findtext('originUrl') print response.findtext('errorCode')

Terms of Service - Privacy Policy - App Statistics - API
Shortening links since early 2006
© 2009 Deft Labs