ColdFusion and the OpenSRS name_suggest API (Part II)

OpenSRS API Add comments

Now we want to post that variable to OpenSRS so we can get our results.  The double-MD5 Hashing stuff they want you to do is pretty arcane, but this stuff needs to be protected.  Here’s my function for posting it to OpenSRS:

<cffunction name="send2OpenSRS" returnType="string" output="false">

<cfargument name="xmlPost" required = "yes">

<cfinclude template="./openSRS_ini.cfm">

<cfset step1 = "#xmlPost##request.privateKey#">
<cfset hashStep1 = "#LCase(hash(step1, 'MD5'))#">

<Cfset step2 = "#HASHSTEP1##request.privateKey#">
<cfset hashStep2 = "#LCase(hash(step2, 'MD5'))#">

<cfhttp 
	method = "Post"
	url = "https://#request.productionServer#"
	port = "#request.usePort#"
	>
	
<cfhttpparam 
	type = "header" 
	name = "X-UserName" 
	value = "#request.userName#">

<cfhttpparam 
	type = "header" 
	name = "X-Signature" 
	value = "#hashStep2#">
	
<cfhttpparam 
	type = "XML" 
	value = "#xmlPost#">
	
</cfhttp>

<cfreturn #XMLParse(cfhttp.filecontent)#>

</cffunction>

Those request variables should be replaced with your own information, of course.  That’s what that CFINCLUDE  file is all about, it just sets the request variables you need: your username, your private key, your production server, the port you are using, etc. 

Anyway.

To use this function you just drop a second CFINVOKE on the page.  Here are the two of them together:

<cfinvoke 					
	component="openSRS.openSRS"
	method="formatRequest_nameSuggest"
	searchstring="#keyword#"
	returnVariable="theRequest"
	>

<cfinvoke 					
	component="openSRS.openSRS"
	method="send2OpenSRS"
	xmlPost="#theRequest#"
	returnVariable="theResult"
	>

Pretty easy to use now, huh?  In the next post, we’ll parse the results into something useful.

(...to be continued)

 

2 responses to “ColdFusion and the OpenSRS name_suggest API (Part II)”

  1. Jon Says:
    This is a great tutorial. Could you also post the openSRS_ini.cfm file?
  2. Michael Cummins Says:
    Thanks! The ini include file I made only has 4 variables in it. You would set them to your particular account info: cfset request.userName = "" cfset request.privateKey = "" cfset request.productionServer = "" cfset request.usePort = ""

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Initial Template Design and Icons by N.Design Studio, modified by Michael Cummins.