I found this article by James Netherton on a discussion list of CF-Brasil group.
I needed that long time ago, when I had to import some data from a PHP web application. I tried his code and it works perfectly, so I’m linking to his blog so you may use that code too if you need.
Invoking a Webservice Using CFHTTP
Here is his code for the component:
{code type=”coldfusion”}
<cfcomponent output=”false” style=”rpc”>
<cffunction name=”addNumbers” access=”remote” returntype=”numeric” output=”false”>
<cfargument name=”firstNumber” type=”string” required=”true”/>
<cfargument name=”secondNumber” type=”string” required=”true”/>
<cfreturn arguments.firstNumber + arguments.secondNumber/>
</cffunction>
</cfcomponent>
{/code}
Here is his code for the test:
{code type=”coldfusion”}
<!— You’d need to strip any whitespace before passing to CFHTTP —>
<cfsavecontent variable=”soap”>
<?xml version=”1.0″ encoding=”UTF-8″?>
<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
<soapenv:Body>
<ns1:addNumbers soapenv:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/” xmlns:ns1=”http://directorypath”>
<firstNumber xsi:type=”xsd:string”>100</firstNumber>
<secondNumber xsi:type=”xsd:string”>10</secondNumber>
</ns1:addNumbers>
</soapenv:Body>
</soapenv:Envelope>
</cfsavecontent>
<!— Note that there’s no ?wsdl appendage to the url —>
<cfhttp url=”http://myserver/webservice.cfc” method=”post”>
<cfhttpparam type=”header” name=”content-type” value=”text/xml”>
<cfhttpparam type=”header” name=”SOAPAction” value=””>
<cfhttpparam type=”header” name=”content-length” value=”#len(soap)#”>
<cfhttpparam type=”header” name=”charset” value=”utf-8″>
<cfhttpparam type=”xml” name=”message” value=”#trim(soap)#”>
</cfhttp>
<!— Dump out a nice representation of the SOAP response —>
<cfdump var=”#xmlParse(cfhttp.FileContent)#”>
{/code}
One thought on “Invoking a Webservice Using CFHTTP”