URL Shortening with Coldfusion and bit.ly - page 2

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Step 3: Parse the returned xml object

When we execute this we will be returned an object with several fields. The field we are interested in is the "filecontent" field which contains the xml object which holds the data we need. So our next step is to turn the returnObject.filecontent into an xmlObject which we can parse.

Note: I specified a return format of XML. If you specify json you will need to follow different step to parse the shortened url out.

<!--- Parse the return object into an xml object --->
<cfset xmlObject=xmlParse(returnObject.filecontent)>

Step 4: Get the shortened url from the xml object

Now that we have an xml object we need to read our short url out of it. The xml object contains several key value pairs however ones we are interested are the "status_code" and "url" values.

The status code indicates if the request was successful or not. A value of 200 indicates success. If we get a 200 code we know we have a shortened url. Check the api documentation for the meaning of other values.

Use the following to get the data from the xmlObject.

<cfset statusCode = xmlObject[1].response.status_code.xmltext>
<cfif statusCode EQ "200">
<cfset shortUrl = xmlObject[1].response.data.url.xmltext>
<cfelse>
<!---something went wrong--->
</cfif>

Conclusion and Final Steps

So there you have it a simple way to automatically get a short url for use with twitter when publishing a post. From here the next step would be to take this shortened url and automatically post it to twitter.

Note: This post only covers the basics and I have not covered error handling here in any detail.

1 2

Reader Comments

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing