Matt Gifford aka coldfumonkeh | Consultant Developer
View Github Profile


Generate QR Codes with bit.ly ColdFusion Component

Oct 14, 2010

This morning I released an updated version of my bit.ly ColdFusion CFC Wrapper, which is now at version 2.0.

The core reason for the update was to provide the new methods now included within the official bit.ly API.

What’s changed?

There are four official new methods that have been added into the ColdFusion wrapper for the API, which are:

  • referers
  • countries
  • clicksByMinute
  • clicksByDay

These methods are a great way to provide a little analytical data for any bit.ly generated URLs. Superb stuff and incredibly easy to obtain, especially though the CFC.

QR Codes

A significant addition to the bit.ly service is the ability to generate Quick Response (QR) codes, or data matrix barcodes - those awesome little 2D barcodes that can be read by most smartphones, scanners etc.. a perfect way of storing and publishing text, information, or most commonly, a URL.

Iron Man QR Code It's not new technology, it's not really a new idea, but with the constant release of smartphones and devices capable of reading them (and so much more) it's becoming more common-place within today's society.

Naturally, as a format widely accessible to not only providers but also consumers, it's become a '2.0' option of delivery for marketing campaigns and advertising - an ideal, enigmatic way to promote your services or products.

Lost QR Code

As reported in Mashable, Google recently released their URL shortening service goo.gl, which by all accounts included QR Code generation from the start. As such, it wasn't a surprise to see bit.ly step up and include this support in their services to ensure that their users weren't missing out on the same capabilities.

Grabbing a QR Code

Although not an official method included within the bit.ly API, ColdFusion makes it incredibly easy to obtain and retrieve this information, it has been included within the bit.ly CFC.

Here's how easy it is to generate the QR Code using the bit.ly CFC:

<!---
	Instantiate the bit.ly object.
--->
<cfset objBitly = createObject('component',
		'com.coldfumonkeh.bitly.bitly')
		.init(
			username	=	'< your account username >',
			apikey		=	'< your bit.ly api key >',
			parse		=	true
		) />


<!---
	The generateQRCode() method returns binary data
	(the byte array) of the generated image.
--->
<cfset qrResponse = objBitly.generateQRCode(
						shortURL	= 'http://bit.ly/d6sCNC'
					) />

<!---
	Dump out the binary response from the
	initial call (just to see what was returned).
--->
<cfdump var="#qrResponse#"
		label="QRCode binary response" />

<!---
	Write the binary data as an image and display
	directly into the browser. Thanks, ColdFusion.
--->
<cfimage action="writeToBrowser"
		 source="#qrResponse#" />

The returned response from the cfdump of the binary data is shown below:

QR Code Binary Dump

And the image generated by passing the binary data into the cfimage tag can be seen here (in this case, this image contains a link to http://www.mattgifford.co.uk):

mattgifford.co.uk QR Code

What's Happening Under The Hood?

Obtaining the generated QR Code is incredibly easy. The guys at bit.ly have engineered their systems to allow anyone to obtain a QR Code by appending the string '.qrcode' to the end of any bit.ly shortened URL, for example: http://bit.ly/d6sCNC.qrcode

Running directly in the browser, you get the generated image returned, which you can then copy / save to suit your needs. The actual call within the CFC Wrapper is very straightforward:


<cffunction name="generateQRCode"
			access="public"
			output="false"
			hint="I take a bit.ly generated URL, and return the
			binary data of the generated QR Code image.">

	<cfargument name="shortURL"
				required="true" type="string"
				hint="I am a short URL that you wish to
						create a QR Code for." />
		<cfset var cfhttp 		= '' />
		<cfset var returnData 	= '' />
			<!---
				Make the cfhttp request to obtain the image
				(appending the string '.qrcode' onto
				the shortened URL)
			--->
			<cfhttp url="#arguments.shortURL#.qrcode" />
			<!--- Successful response? --->
			<cfif cfhttp.StatusCode EQ '200 OK'>
				<!---
					Obtain the binary data using the
					toByteArray() method
				--->
				<cfset returnData =
						cfhttp.fileContent.toByteArray() />
			</cfif>

	<cfreturn returnData />

</cffunction>

How do I get it?

You can download the revised version of the API wrapper from bitly.riaforge.org


Latest Blog Posts

Jul 16, 2020
Github Actions with CommandBox and TestBox
Read More
Jul 9, 2020
Azure pipelines with CommandBox and TestBox
Read More
Dec 23, 2019
CFML content moderation detection component library
Read More