Skip to main content
Dileep_NR
Inspiring
May 21, 2010
Question

How to use SOAP in ColdFusion

  • May 21, 2010
  • 3 replies
  • 10138 views

Hi All,

The code set am using as follows

--------
index.cfm
--------
<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"
        xmlns="http://one.dileep.com/soaptest/"><!---1 this is the cfc location--->

    <soapenv:Header>
    <setInit>1</setInit>
    <!--- 2 header param --->
    </soapenv:Header>
    <soapenv:Body>
        <addNumbers soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" >
        <!---3 cfc method name --->
            <firstNumber>1000</firstNumber>
            <secondNumber>10</secondNumber>
        </addNumbers>
       </soapenv:Body>
</soapenv:Envelope>
</cfsavecontent>

<cfhttp url="http://one.dileep.com/soaptest/soaptest.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>

<cfdump var="#xmlparse(cfhttp.FileContent)#">

-------------
soaptest.cfc
-------------
<cfcomponent output="false" >
    <cffunction name="addNumbers" access="remote" returntype="any" output="false">
        <cfargument name="firstNumber" type="string" required="true"/>
        <cfargument name="secondNumber" type="string" required="true"/>
        <cfset retunVal="NO">
        <cfset username=0>
        <cfif isSOAPRequest()>           
            <cfset username = getSOAPRequestHeader("http://one.dileep.com/soaptest/", "setInit")>
            <!---4 retrieving  SOAP header value --->
        </cfif>
       
        <cfreturn arguments.firstNumber + arguments.secondNumber & username   />

    </cffunction>

</cfcomponent>


This is the right way for implimenting SOAP in COLDFUSION webservice ?

More Questios

1- <!---1 this is the cfc url--->

What is the relevance of the cfc URL ? I am using sub domains

eg : one.dileep.com, two.dileep.com,yyy.dileep.com, in this case the cfc url 'http://<<subdomain>>.dileep.com/soaptest/' should changed accordingly?

2-<!--- 2 header param --->

We have to give any tag name as Header?

3- <!---3 cfc method name --->

SOAP body  tag must be the method name that we need to exicute?

Here, I have to execute method 'addNumbers'

4- <!---4 retrieving  SOAP header value --->

While retrieving the header value should we need to specify the cfc url?('http://<<subdomain>>.dileep.com/soaptest/')

Please help.

This topic has been closed for replies.

3 replies

BKBK
Community Expert
Community Expert
May 31, 2010

<!--- Vital: No space between cfsavecontent tag and xml processing instruction element --->

<cfsavecontent variable="soap"><?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://one.dileep.com/soaptest/">
    <SOAP-ENV:Header>
        <setInit>1</setInit>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <addNumbers SOAP-ENV:EncodingStyle="http://schemas.xmlsoap.org/soap/encoding/" >
            <firstNumber>1000</firstNumber>
            <secondNumber>10</secondNumber>
        </addNumbers>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</cfsavecontent>

<!--- URL of the WSDL not of the CFC! --->

<cfhttp url="http://one.dileep.com/soaptest/soaptest.cfc?wsdl" 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>

Inspiring
May 26, 2010

Have you read the chapter in CFWACK vol 3 on working with webservices?  It's not very in-depth, but has some pointers and the usual lame-a** code example.

Depending on the complexity of what you are trying to do, you might be better off building a RESTful service instead of a SOAP service.  Despite it's name, SOAP tends to be overkill if all you are doing is passing a couple of args to the webservice, but is good if the request is complex.  REST is easiest if you are just sending a couple of args, but a pain in the a** if you are trying to do something complex in a single operation (works better to break down into individual state-changing calls to individual webservices).

A couple of comments on what you are doing in your code:

It's a lot simpler to use CFINVOKE than to roll your own webservice HTTP call.  If you're writing your own webservice this should be especially true.  I've ended up using CFHTTP to call vendor's webservices that I had no control over and wouldn't work with CFINVOKE, but that is the case where I've seen that to be true.  Check out the example in CFWACK.

If you continue on the CFHTTP route, you might find it easier to use the CFXML tag instead of using CFSAVECONTENT to create the XML variable - this will allow CF to validate the xml document and ns references.  It also allows you to use the CFDUMP tag to get a formatted view of the xml doc to help with debugging.

Most of the CFHTTPPARAMS you have included in your call are not really needed, at least that's been my experience.  For example, this is all I ever use in my SOAP calls:

   <cfhttp url="https://adwords.google.com/api/adwords/cm/v200909/AdGroupAdService" method="post">
    <cfhttpparam name="SOAPAction" type="header" value=""/>
    <cfhttpparam type="xml" value="#ToString(variables.adAd)#"/>
   </cfhttp>

"variables.adAd" is the xml doc created by this code:

   <cfxml variable="adAd">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v20="https://adwords.google.com/api/adwords/cm/v200909" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
       <soapenv:Header>
          <v20:RequestHeader>
               authorization data goes here
          </v20:RequestHeader>
       </soapenv:Header>
       <soapenv:Body>
               magic happens here
       </soapenv:Body>
    </soapenv:Envelope>
   </cfxml>

Hope this helps.  My experience in this is that it requires some trial and error and lots of CFDUMPs to get it right the first time, but after you get over that initial hurdle it all suddenly "makes sense" from then on.

Reed

May 29, 2010

Hi Reed,

I sort of stumbled into this thread while searching for some help resources on writing a CF client for the adwords api v200909 and noticed that you used it in your example.  I am still early in the process.  I have looked briefly at the documentation at the  google site, and it looks a bit daunting.  I know that there is a sample CF code out there, but it's for v12.  Do you know whether it's a good starting point or whether too much has changed since that version?  And do you know any other relevant CF resources?  I haven't had much luck in my google searches.

Thanks!

Update: I found this post that points to some xml examples.  Looks promising so I am going to give it a try.

http://groups.google.com/group/adwords-api/browse_thread/thread/c3ed0d9aab8be6e7/fc987d599b4b074d?lnk=gst&q=coldfusion#fc987d599b4b074d

Min

Participant
July 21, 2010

This post has an example of using SOAP in ColdFusion for the Adwords API v201003


http://www.jensbits.com/2010/07/18/clicks-and-impressions-from-google-adwords-api-using-coldfusion/

Dileep_NR
Dileep_NRAuthor
Inspiring
May 24, 2010

Hi All,

     Any help on this.....