Generate QR Codes with bit.ly ColdFusion Component

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

7 Comments

Leave a Comment
  1. Hi Matt,

    I tried to pass an argument to objBitly.gererateQRCode rather than hardcoding the short URL by using:

    qrCode = objBitly.generateQRCode(shortURL=shorten.response.data.url);

    But got an error:
    coldfusion.runtime.TemplateNotFoundException: File not found: /testing/error.cfm

    Any idea why it does not work?

    Thanks.

  2. Hi Nelson

    Thanks for your comment. The TemplateNotFoundException error would seem to indicate an error somewhere with perhaps a mapping on your server, as it’s trying to display an error page.

    The argument passed in to the generateQRCode() method as shown in your comment would fail. There are two types of return method, JSON and XML. If you are using the XML output, you will need to add an extra identifier to return the string value from the XML, like so:

    qrCode = objBitly.generateQRCode(shortURL=shorten.response.data.url.XmlText);

    If you are using the JSON output format, the argument would need to resemble this:

    qrCode = objBitly.generateQRCode(shortURL=shorten.data.url);

    I hope this helps. :)

  3. Hi,

    found your incredible bit.ly API and tried to generate a QR Code in CF9. Unfortunately I get the error “ColdFusion was unable to create an image from the specified source file.Ensure that the file is a valid image file.”

    What am I doing wrong please? Any ideas?

    Thanks very much, greetings
    Bernhard

    1. Sorry, forgot to post the dump:

      37806870454946521037-30-29-49-451050324832111981063260604784121112101478879981061019911647671111081111148311297991014768101118105991018271664783117981161211121014773109971031014766105116115801011146711110911211111010111011632564787105100116104325548484776101110103116104325351575551504772101105103104116325157514770105108116101114477010897116101681019911110010162621151161141019710910120-100-116-6771152887-106-25-5135494108-20-1876-8537-111-12445120-1765-12878363712282941065161-8311073-3550203781-123052328-171185-16-90803064-639112215-17-5459248245117-9-104-9-30-19-68-19119-5061-103-731869-11-1868-100-7211351435143-8564-42-7-27-1-104599911379-1019351-3056-75-24-1249-39-44-9499-58219-40-29-60-3-117-98-87555-33319-102-619752-46100-42-22-95-21-52-8-82-20-28-1217379-124-105102-529448-72502957103-15-52-102113-100-77-7210345-11457-85-109108-47-22-10255592244-1869-85-57108-13-627254111-5935-25-39454-23-1286911339108-62-6322-51829-55109-63-26-9923-82-2950-37-36112239247644118-113-43-315-2919-10291-8411048-1012955-5178-17-94-61-12559-47929686-69-53-26-6487-19-1143748-10456-100-5378-4110-11696-76317060-35-261-291948-77546-7810011844-6145-4713-120111-19-74-69-68118-41-78-45-771035-104-51-71-124-101-30101-7-59-55-124-37-77121104-126-90-89596152-62-719611260-369-20-3170311614118123-105-26222275-544285-19-122-2622-6792-47-42-4010880107123-55-3853-9058121-13-3963-98-72118-2898-47-123-78-36-7769-5912153-118-54-744387-117106-21-38-22-2285-1113-199677-8358-8011523115-49-100-65-102119-93-5298-11122-67-4993-8221-89-451138-36-11699-119-6628-40-17118-81-709275110-9-78-41-697435-104-57-7726-10114-571812569-131111812023101459067-93-9077-43-47-82-1810687119-126-87539326-8373-8351105-12-359093-10578-41-47105-205225-69-65876-67-122-5062125-105-8273-10711949-7386-3412395454105-10191134260-7375-85-21110105-4784864279-99-70-16-47-17-662425123-72-76-63-499-112-2-35-23-101-79-38-106-32-125-128912750-11723-5210293-78-37-1069768-7747-7787-105-247224-313127-3-109-125-111-971112723-77-595619-38-9211-149-67686115122-11994-5977-104-77-476-54546-101-17-12630122107-104-104-15-97-112112-61116-121118-117-37102118-394578-85-4341-68-812914-61-119-1152592-112-829-57-61-25-94-69-59107-11831-109-10-93-119-33312494-297-55-20118-8-5-62-33111105-59-77-12124121-11-39-46-54-77-9185-7821-49-14-7827-1047-13-55-7827-2-52-5375110-887-7912111-761041184744-30-41-78104118-50-1019311511104-13-117110-102-524656102-26-195111514110-4511-50-5557-365159-25-128-9-123127-7294-89-5-103123105-43-27-12311995-1444-63-98101-1750-103-41-75-68-2892118-37609630-69-41-29112123-993048-70-10985-171076-56-98-71-105-97123112197811959-359630-10510373-6832-52-99118-89-53-311148893-4269-89101-345355-17
      [truncated]

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>