Entries Tagged as 'OpenSRS API'

ColdFusion and the OpenSRS name_suggest API (Part III)

OpenSRS API 1 Comment »

Now we have to parse the results.  In my example, I wanted results for the straight lookup, the premium names and the suggested names, so I take the XML and use it to make three queries.  That part looks like this:

<!--- LOOKUP --->

<cfset searchFor = XMLSearch(theResult, "//data_block/dt_assoc//dt_assoc//item[@key='lookup']//dt_assoc//item[@key='items']//dt_array/item/dt_assoc/")>

<cfset countLookups = ArrayLen(searchFor)> 
 
<!--- Let's create a query to hold the actual lookup data --->

<cfset qLookup = QueryNew("domainName, result")>

<cfif #countLookups# GTE 1>

<cfset temp = QueryAddRow(qLookup, countLookups)>

<cfloop from="1" to="#countLookups#" index="thisRecord">

<cfset lookup1 = #searchFor[thisRecord].item[1].XmlText#>
<cfset lookup2 = #searchFor[thisRecord].item[2].XmlText#>

<cfset temp = QuerySetCell(qLookup, "domainName", lookup1, thisRecord)>
<cfset temp = QuerySetCell(qLookup, "result", lookup2, thisRecord)>

</cfloop>

</cfif>

<!--- PREMIUM --->

<cfset searchFor = XMLSearch(theResult, "//data_block/dt_assoc//dt_assoc//item[@key='premium']//dt_assoc//item[@key='items']//dt_array/item/dt_assoc/")>

<cfset countPremiums = ArrayLen(searchFor)> 

<!--- Let's create a query to hold the actual lookup data --->

<cfset qPremium = QueryNew("domainName, result, price")>

<cfif #countPremiums# GTE 1>

<cfset temp = QueryAddRow(qPremium, countPremiums)>

<cfloop from="1" to="#countPremiums#" index="thisRecord">

<cfset lookup1 = #searchFor[thisRecord].item[1].XmlText#>
<cfset lookup2 = #searchFor[thisRecord].item[2].XmlText#>
<cfset lookup3 = #searchFor[thisRecord].item[3].XmlText#>

<cfset temp = QuerySetCell(qPremium, "domainName", lookup1, thisRecord)>
<cfset temp = QuerySetCell(qPremium, "result", lookup2, thisRecord)>
<cfset temp = QuerySetCell(qPremium, "price", lookup3, thisRecord)>

</cfloop>

</cfif>

<!--- SUGGESTION --->

<cfset searchFor = XMLSearch(theResult, "//data_block/dt_assoc//dt_assoc//item[@key='suggestion']//dt_assoc//item[@key='items']//dt_array/item/dt_assoc/")>

<cfset countSuggestions = ArrayLen(searchFor)> 

<!--- Let's create a query to hold the actual lookup data --->

<cfset qSuggestion = QueryNew("domainName, result")>

<cfif #countSuggestions# GTE 1>

<cfset temp = QueryAddRow(qSuggestion, countSuggestions)>

<cfloop from="1" to="#countSuggestions#" index="thisRecord">

<cfset lookup1 = #searchFor[thisRecord].item[1].XmlText#>
<cfset lookup2 = #searchFor[thisRecord].item[2].XmlText#>

<cfset temp = QuerySetCell(qSuggestion, "domainName", lookup1, thisRecord)>
<cfset temp = QuerySetCell(qSuggestion, "result", lookup2, thisRecord)>

</cfloop>

</cfif>

The only thing really hairy about that is doing the XMLSearch for the XPATH.  That was a little odd for me the first go around, but the rest of it is pretty standard fare.

That’s it!  You started off with just a keyword, and now you have 3 queries you can display.   Here they are CFDUMPed.  The only thing left to do is make it pretty!  I’ll leave that up to you.



ColdFusion and the OpenSRS name_suggest API (Part II)

OpenSRS API 2 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)

 

ColdFusion and the OpenSRS name_suggest API (Part I)

OpenSRS API No Comments »

Recently OpenSRS made some changes to their name_suggest API, and I decided to write a cold fusion component to make using it a whole lot easier.  In case someone else out there is interested in using ColdFusion to interact with the OpenSRS API, I thought I would post some of the basics here. 

I kind of threw this together, so it isn’t very feature-rich, but it’s a great starting point if you’re interested.

Anyway.  The process is this: 1) You format the XML request that you’re going to send , 2) you send it over https, and 3) you parse the results into something meaningful.  You will, of course, have to be a OpenSRS reseller with a username and a password.

Here’s the documentation on the API:
http://opensrs.com/resources/domains/documentation/

We’re using the name_suggest function.

First you will need some keywords.  It can be a domain name like cfjazz.com, or it can be something wordy, like "cold fusion jazz".  Here’s the function that formats the XML request that you’re going to send.

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

<cfargument name="searchstring" required	= "yes">
<cfargument name="lookup" required	= "no">
<cfargument name="suggestion" required	= "no">
<cfargument name="premium" required	= "no">

<cfparam name="lookup" default = "yes">			
<cfparam name="suggestion" default = "yes">			
<cfparam name="premium" default = "yes">		

<cfset services_list = "">

<cfif #lookup# is "yes">			
 <cfset services_list = listAppend(services_list, "lookup")>				
</cfif>

<cfif #suggestion# is "yes">	
 <cfset services_list = listAppend(services_list, "suggestion")>		
</cfif>

<cfif #premium# is "yes">			
 <cfset services_list = listAppend(services_list, "premium")>			
</cfif>

<cfset services_count = ListLen(services_list)>

<cfsavecontent variable="thePost"><?xml version='1.0' encoding='UTF-8' standalone='no' ?>

<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>

<OPS_envelope>

<header>

<version>0.9</version>

</header>

<body>

<data_block>

<dt_assoc>

<item key="protocol">XCP</item>

<item key="action">name_suggest</item>

<item key="object">domain</item>

<item key="attributes">

<dt_assoc>

<item key="searchstring">#searchstring#</item>

<item key='languages'>

<dt_array>

<item key='0'>en</item>

</dt_array>

</item>

<item key="tlds">

<dt_array>

<item key="0">.com</item>

<item key="1">.net</item>

<item key="2">.org</item>

<item key="3">.biz</item>

<item key="4">.us</item>

<item key="4">.mobi</item>

</dt_array>

</item>

<item key="services">

<dt_array>

<cfloop from="1" to ="#services_count#" index="tehCount">

<item key="#NumberFormat((tehCount - 1), "9")#">#ListGetAt(services_list, tehCount)#</item>

</cfloop>

</dt_array>

</item>

<item key="maximum">25</item>

</dt_assoc>

</item>

</dt_assoc>

</data_block>

</body>

</OPS_envelope></cfsavecontent>

<cfset trimmedPost = #trim(thePost)#>

<cfreturn #trimmedPost#>

</cffunction>

Yeah, that looks like a lot, but it isn’t really.  It takes the search string you passed it, lets you tell it YES or NO for the lookup, suggestion and premium services and then formats the XML with a CFSAVECONTENT.  If I wanted to improve this, I’d add more CFARGUMENTS to let me pass more of the name_suggest parameters, which can be found here:

http://opensrs.com/resources/documentation/opensrsapixml/name_suggest_domain_request.htm

This will do for now though.  So, to use this function, you just drop a CFINVOKE on the page you post your keywords to.  Like this:

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


Pretty straightforward, eh?  You give the CFINVOKE your keywords, and it hands you back some XML in a variable.

(...to be continued)

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