Skip to main content
Participant
October 29, 2010
Question

Web Sevices/API

  • October 29, 2010
  • 4 replies
  • 1449 views

I'm using ColdFusion 8. I need help with Web Services and APIs. I can get the simple a convernsion from Celsius to Fahrenheit to work:

<cfinvoke

webservice = "http://webservices.daehosting.com/services/TemperatureConversions.wso?WSDL"

method="CelciusToFahrenheit"

nCelcius = "54"

returnvariable="foo"

>

<cfoutput>#foo#</cfoutput>

I can't get anything else to run. Is there a standard template, tool of dvelopment style that I can use to fill in information? Bellow is an API link for SPRACI, it is open an does not require an API key. Can some show me how to intergrate the functions into a web page. Or provide me with other examples that I can study? Thanks

Site with the SPRACI API:

http://www.spraci.com

http://www.xmethods.net/ve2/ViewListing.po;jsessionid=kDmNk9mBY6I2x7C9VJMbvmEl?key=uuid:859FF87B-E535-4B29-4E16-ADEC0589B599

http://www.spraci.com/services/soap/index.php?wsdl

Operation / Method Name

area_list

area_getinfo

event_list

event_getinfo

event_show

event_add

event_update

user_getinfo

user_getarea

    This topic has been closed for replies.

    4 replies

    bigpapa2Author
    Participant
    November 1, 2010

    I'm attempting to learn how to manage Web Services/APIs. I'm using ColdFusion 8 and the webservice is open (no key or password required). With my createobject I don't see where to include the operation or method (see list below) like was done with the <cfinvoke>. With <cfinvoke> I had save the document as wscfc.cfm is it the same with createobject? Thanks for the suggestions TheRealAgentK.

    At the bottom of this page is a “Second Attempt” using the Put and <cfinvoke>. This modified code that was sent to me by BKBK (Thanks!). The error message follows the code.

    I'm getting an error message (see below). Any help with this API or sample connections would be greatly appreciated.

    ?WSDL link below:

    http://www.spraci.com/services/soap/index.php?wsdl

    My script below (operation/method = area_list):

    <cfscript>

    stUser = structNew();

    stUser.appkey = "";

    stUser.username = "";

    stUser.password = "";

    stUser.area = "x0usa";

    stUser.name = "";

    ws = createObject("webservice", "http://www.spraci.com/services/soap/index.php?wsdl");

    myReturnVar = ws.echoStruct(stUser);

    </cfscript>

    <cfoutput>#myReturnVar#</cfoutput>

    Link to API notes:

    C:\Users\djones\Documents\Affiliates\spraci\Webmaster Services SPRACI API SOAP area_list.mht

    Operations /Methods below?

    http://www.xmethods.net/ve2/WSDLAnalyzer.po;jsessionid=GuNmIhBRDeHT2vRN_L4c7nK8

    area_list

    area_getinfo

    event_list

    event_getinfo

    event_show

    event_add

    event_update

    user_getinfo

    user_getarea

    Error Message Below:

    Web service operation echoStruct with parameters {{USERNAME={},PASSWORD={},AREA={x0usa},APPKEY={},NAME={}}} cannot be found.

    The error occurred in C:\ColdFusion8\wwwroot\ybah8r\CMS\Progs\eraseme3.cfm: line 17

    15 : stUser.name = "";
    16 : ws = createObject("webservice", "http://www.spraci.com/services/soap/index.php?wsdl");
    17 : myReturnVar = ws.echoStruct(stUser);
    18 : </cfscript>
    19 : 

    Second attempt:

    <cfset AreaFetch = createobject("java","java.util.Hashtable")>

    <cfscript>

    AreaFetch.put("appkey", "");

    AreaFetch.put("username", "");

    AreaFetch.put("password", "");

    AreaFetch.put("area", "x0usa");

    AreaFetch.put("name", "");

    </cfscript>

    <cfinvoke

    webservice = "http://www.spraci.com/services/soap/index.php?wsdl"

    method="area_list"

    inputStructArray ="#AreaFetch#"

    returnvariable="foo"

    >

    <cfdump var="#foo#">

    Error message:

    Web service parameter name data cannot be found in the provided parameters {INPUTSTRUCTARRAY}.

    The error occurred in C:\ColdFusion8\wwwroot\ybah8r\wscfc.cfm: line 41

    39 : webservice = "http://www.spraci.com/services/soap/index.php?wsdl"
    40 : method="area_list"
    41 : inputStructArray ="#AreaFetch#"                        
    42 : returnvariable="foo"
    43 : >
    BKBK
    Community Expert
    Community Expert
    November 1, 2010

    Are you sure one can invoke the web service with empty string data? Here is an example of the SOAP method with TheRealAgentK refers to:

    <!--- WSDL --->
    <cfset wsdl_url="http://www.spraci.com/services/soap/index.php?wsdl">
            
    <cftry>
        <!--- SOAP message to send to Web Service--->
        <cfsavecontent variable="msg"><?xml version="1.0" encoding="UTF-8" ?>
            <cfoutput><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>
             <tns:data soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.spraci.com/services/soap/">
                  <appkey xsi:type="xsd:string">1</appkey>
                  <username xsi:type="xsd:string">BKBK</username>
                  <password xsi:type="xsd:string">BKBK_pw</password>
                  <area xsi:type="xsd:string">USA</area>
                  <name xsi:type="xsd:double">USA</name>
              </tns:data>
              </soapenv:Body>
              </soapenv:Envelope></cfoutput>
        </cfsavecontent> 
       
        <!--- Invoke web service and send message--->
        <cfhttp url="#wsdl_url#" 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(msg)#">
            <cfhttpparam type="header" name="charset" value="utf-8">
            <cfhttpparam type="xml" name="area_listRequest" value="#trim(msg)#">
        </cfhttp>
       
        <!--- Web Service response --->
        <p><cfoutput>#xmlFormat(cfhttp.fileContent)#</cfoutput> </p>
           
    <cfcatch type="any">
        <cfdump var="#cfcatch#">
    </cfcatch>
    </cftry>

    bigpapa2Author
    Participant
    November 6, 2010

    BKBK and The RealAgentK,

    Much thanks. I'm just getting back to because the email went into my spam

    account. I'll look into the empty string data.

    Bigpapa2

    Participating Frequently
    October 31, 2010

    Bigpapa2,

    have a look at the WSDL code provided by the service you want to call. All the services of the links you've provided rely on complexType definitions in the XML schema of the WSDL for sending their data.

    Example:

    area_getinfo expects a SOAP-packaged XML document fitting this complexType definition:

    <xsd:all>
    <xsd:element name="appkey" type="xsd:string"/>
    <xsd:element name="username" type="xsd:string"/>
    <xsd:element name="password" type="xsd:string"/>
    <xsd:element name="area" type="xsd:string"/>
    <xsd:element name="name" type="xsd:string"/>
    </xsd:all>
    </xsd:complexType>

    In your code you have to create a document structure that could be transferred into an XML document matching this by Apache Axis (the internal WS engine of CF). In this case a simple struct should do:

    <cfscript>

    abc = structNew();

    abc["appkey"] = "whatever";

    ...

    </cfscript>

    If that doesn't work, try using JavaCast to make sure the typecasts get done correctly to String.

    Also: Please read this article - it's the must read for everyone who's trying to deal with complex data structures in webservices consumed by CF:

    http://tjordahl.blogspot.com/2008/04/reprint-consuming-web-service-complex.html

    Cheers

    Kai

    BKBK
    Community Expert
    Community Expert
    October 30, 2010

    Might something like this work?

    <cfset AreaFetch = createobject("java","java.util.Hashtable")>
    <cfscript>
    AreaFetch.put("appkey", "the_appkey");
    AreaFetch.put("username", "the_username");
    AreaFetch.put("password", "the_password");
    AreaFetch.put("area", "the_area");
    AreaFetch.put("name", "the_name");
    </cfscript>
    <cfinvoke
    webservice = "http://www.spraci.com/services/soap/index.php?wsdl"
    method="area_getinfo"
    inputStructArray ="#AreaFetch#"                       
    returnvariable="foo"
    >

    <cfdump var="#foo#">

    Inspiring
    October 30, 2010

    What method are you trying to run?

    What code are you using to run it?

    What happens when you run that code?