Page 1 of 1

Creating an XML client - what extensions to use?

Posted: Wed Mar 24, 2010 12:28 pm
by benward
Hello,

I have been tasked with creating a new web application for internal use at my office. This application has to pull data in from an external website. In order to achieve this we have asked the owners of the external website to produce an xml web service to run over https, which they will write in php. I intend to open up port 443 on our firewall so that our web server can contact this web service.

firstly, my server config:
PHP Version: 5.3.2
Display Errors: On
Error Level: Not E_ALL
Register Globals: Off





Now the XML request that I need to send is very specific - and no wsdl file is available. Please see below:

Code: Select all

 <?xml version="1.0" ?>   <CustomerDataRequest>      <SystemAuthentication>          <username>xxxxxxxxxx</username>           <password>xxxxxx</password>       </SystemAuthentication>      <ReferenceNumber>TVL1000582468</ReferenceNumber>   </CustomerDataRequest> 
I have attempted to achieve this in classic ASP by very simply loading the above xml file as an xml dom object and sending it along in the get/post of asp's version of curl. This worked perfectly.

I have now been told by my boss that we need to move away from old languages such as classic ASP (which I agree with wholeheartedly) and instead I am to do this in PHP.

I'm quite sure that this will be simple enough to achieve in PHP if I use curl, but I dont want to. The only reason I did it like that in ASP is because there was no other way to do it. I want to do it 'better' if that makes sense. PHP has all sorts of extensions for soap and for XML and I want to use them - to future proof myself and my code if for nothing else.

The question is; what extension(s) shall I use? I initially tried PEAR SOAP only to find that it placed everything in the XML into a root node called <item> - which the service at the other end did not like. After some digging I discovered that PEAR had all but abandonned the PEAR SOAP module in favour of the native soap support that had been added to PHP.

I am currently looking at the PHP SOAP extensions - which look better - but it is still formatting the xml requests in the 'wrong' format.

So far the code I have is like this:

Code: Select all

 <?phpfunction showarray($input){    echo "<ul>";    foreach($input as $key => $loop){        echo "<li>Index <font color='purple'><b>" . $key . "</b></font> is of type <font color='green'><b>" . gettype($loop) . "</b></font> of value";            if (gettype($loop) == "array") {                echo "s:<ul>";                showarray($loop);                echo "</ul></li>";            } elseif (gettype($loop) == "boolean") {                if ($loop == false) {                    echo ": <font color='red'><b>False</b></font></li>";                                } else {                    echo ": <font color='red'><b>True</b></font></li>";                }            } elseif (gettype($loop) == "string") {                echo ": \"<font color='navy'><b>" . $loop . "</b></font>\"</li>";                       } else {                echo ": <font color='orange'><b>" . $loop . "</b></font></li>";            }    }    echo "</ul>";    return;}   #This is a php webpage designed to test some XML  define ("SOURCE_URL","http://xxxxxxxxxxxxxxxx");define ("SOURCE_URI","http://xxxxxxxxxxxxxxxx"); $getclient = new SoapClient (null, array    ('location' => SOURCE_URL,                                            'uri' => SOURCE_URI)        ); $Authentication["username"] = "test1234";$Authentication["password"] = "aabcd"; $CustomerDataRequest["SystemAuthantication"] = $Authentication;$CustomerDataRequest["ReferenceNumber"] = "TVL1000582468"; $params = array("CustomerDataRequest" => $CustomerDataRequest); showarray($params); $response = $getclient->soapCall("", $params); echo "<pre>\n";print_r($response);echo "</pre>\n";?>  
The showarray function is just something I put together for easy debug - I don't like the print_r output very much!

As you can see my array has been built as per the XML file but the XML generated by the PHP SOAP extensions contains a huge amount of text that is superfluous to requirements (and causes the server to fall over!)

Code: Select all

 <?xml version="1.0" encoding="UTF-8" ?>     <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://192.168.91.76:8081/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">    <SOAP-ENV:Body>        <ns1:soapCall>            <param0 xsi:type="xsd:string" />             <param1 xsi:type="ns2:Map">                <item>                    <key xsi:type="xsd:string">CustomerDataRequest</key>                     <value xsi:type="ns2:Map">                    <item>                          <key xsi:type="xsd:string">SystemAuthantication</key>                         <value xsi:type="ns2:Map">                            <item>                                <key xsi:type="xsd:string">username</key>                                 <value xsi:type="xsd:string">test1234</value>                             </item>                            <item>                                <key xsi:type="xsd:string">password</key>                                 <value xsi:type="xsd:string">aabcd</value>                             </item>                        </value>                    </item>                    <item>                        <key xsi:type="xsd:string">ReferenceNumber</key>                         <value xsi:type="xsd:string">TVL1000582468</value>                     </item>                    </value>                </item>            </param1>        </ns1:soapCall>    </SOAP-ENV:Body>  </SOAP-ENV:Envelope> 

Is it possible to specify the format of the XML? Or am I going about this in completely the wrong way?

Any advice or assistance would be most appreciated. Many thanks for reading.

V.B.R. Ben

Re: Creating an XML client - what extensions to use?

Posted: Wed Mar 24, 2010 4:19 pm
by Sofw_Arch_Dev
I'm curious how you can call a web service without a WSDL or without a specifying function name.

Code: Select all

 
$response = $getclient->soapCall("", $params);
 
You aren't calling any function here. How would the SOAP service know what to do with your request?
I have attempted to achieve this in classic ASP by very simply loading the above xml file as an xml dom object and sending it along in the get/post of asp's version of curl. This worked perfectly.
Why can't you do what you did in ASP but just use PHP to create the XML structure (using SimpleXML, say) and execute the curl? What made you decide to use SoapClient?

Re: Creating an XML client - what extensions to use?

Posted: Wed Mar 24, 2010 10:00 pm
by yacahuma
If you get a wsdl I have a class generator from wsdl that works very well and very clean.

http://www.stccorp.net/soapwriter/soapwriter.php

Re: Creating an XML client - what extensions to use?

Posted: Fri Mar 26, 2010 1:20 pm
by Sofw_Arch_Dev
yacahuma,

thank you for that URI. I used it on on WSDL I've been unsuccessful at writing a client for, and the code generated by your utility worked very well. It revealed what I may have been doing wrong in my own code.

Re: Creating an XML client - what extensions to use?

Posted: Fri Mar 26, 2010 3:09 pm
by yacahuma
your welcome. I wrote this thing and it has been extremely usefull to me. I dont know if there is something out there to do the same thing. When I started , I searched and found nothing. But I dont here people complaining that there isnt such a tool. So either people are coding by hand or using something else I dont know about.

Re: Creating an XML client - what extensions to use?

Posted: Mon Mar 29, 2010 5:46 am
by benward
Thanks Guys.

This is why I posted, I didn't have any particular reason for choosing soapClient other than the fact that one of our test services needed a soap header so I was mucking around with that, plus the training manual I have uses PEAR SOAP for all of it's xml examples... I want to make sure that I am using up to date techniques. If this SimpleXML is considered a decent up to date technique to use then I'll give that a shot.

VBR
Ben

Re: Creating an XML client - what extensions to use?

Posted: Mon Mar 29, 2010 12:46 pm
by Sofw_Arch_Dev
From your original post:
I have attempted to achieve this in classic ASP by very simply loading the above xml file as an xml dom object and sending it along in the get/post of asp's version of curl. This worked perfectly.
I have now been told by my boss that we need to move away from old languages such as classic ASP (which I agree with wholeheartedly) and instead I am to do this in PHP.
I asked this before, but maybe you missed it. Why are you not simply rewriting your working ASP code in PHP? Seems cut and dried to me. So far your only reasons for using SoapClient are 1) a test SOAP service whose applicability here is currently tenuous since we don't know how it relates to your current task, and 2) some PEAR examples of which we can say the same. Did the service you're submitting the XML to in ASP change to use SOAP?

If not, use SimpleXML to create the XML structure, and then leverage PHP cURL to POST the XML like you did in ASP.

Re: Creating an XML client - what extensions to use?

Posted: Tue Mar 30, 2010 3:16 am
by benward
I've done it now with Curl, just having a few problems processing the response.

Just being very new to PHP, I didn't know what I should be using and just went on what my training manual said.

I'm sending the XML in the CURL post array and it seems to be working a treat :)

Thanks
Ben