Below is the source code for connecting to and XML Web service (SOAP) using PHP. Within the code is all the info you will need hopefully.
PHP 4.3.4(5) Windows 2000 (IIS) all dll's copied from the php folders into \winnt\system32 folder
Code: Select all
<?php
// Web Service Example Client which connects to http://helena.europe.webmatrixhosting.net/Morse.asmx
// This client connects to a SOAP Web Service which takes a users input and displays the equivalent morse code
// This little piece of code was built to show how easy it really is to conect to a web-service using the php_curl extension
// I spent about 4 weeks with PEAR and NuSOAP trying to connect to multiple web services with no joy
// I then spoke to Wierdan from the devnetwork forum about using php to connect to a standard XML-RPC and this was the eventual outcome
// Hopefully this will help developers all around the world develop clients using php instead of that nasty .NET crap!
// I will bring you the code to use sablotron to XSLT the result that this code brings back as soon as I have 5 minutes to work on it
// Also, in about 2 months I will be bringing out and e-book that basically takes you through some more WS clients
// some of which connect to simple xml-rpc WS and some (as in this case) that connect to a SOAP stream
// The WSDL etc. that I use below can be found @ http://www.xmethods.com/ve2/ViewListing ... y(QhxieSRM)?key=uuid:0088C372-B21D-D8E8-CF0B-56FB10A99EDE
// First thing's first: Set up the actual SOAP Envelope you want to send and assign it to a variable
// In this case we want to send the words Steven Freeman (thats me that is) to the WS and receive the equivelant in morse code
$xmlvars='<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:s0="http://www.regom.de"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<s0:MsgtoMorse>
<s0:msg>Steven Freeman</s0:msg>
</s0:MsgtoMorse>
</soap:Body>
</soap:Envelope>';
// Next we want to set up cURL to send out the SOAP message and catch the response as a variable
$ch=curl_init("http://helena.europe.webmatrixhosting.net/Morse.asmx"); // This tells cURL where the actual WS sits (in otherwords the aplication that sits on a server waiting for you to send it the SOAP request
curl_setopt($ch,CURLOPT_HTTPHEADER, Array("Content-Type: text/xml; charset=UTF-8","SOAPAction: http://www.regom.de/MsgtoMorse")); //Set up the Content Types and SOAPActions
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); // Only required when connecting to a WS on an HTTPS server
// curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); // Only required when connecting to a WS on an HTTPS server
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xmlvars); // Send the Soap Envelope
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($ch); // Hold the response as a variable (in this case called $result)
echo $result; // Echo out the pure XML
// As I said, showing the response as pure XML definately wont be good enough for your boss
// So I will get back to you with the Sablotron Tranformation info as soon as.
// If you implement and web client using this code, there is a possibility it will look like
// nothing has happened even though you aren't receiving any errors in your php logfile. This
// is because if the XML is in a pure state it may be possible that it will be brought back
// in the format such as <fname ="steven" /><sname ="Freeman" />. Now to see this type of XML RS
// you will have to right click on the page in your browser and view the source code.
// Hope this helps
// Regards, Steven Freeman
?>Code: Select all
<?php
if(count($_POST) == 0) {
echo '<form name="form1" method="post" action=' . $PHP_SELF . '>
<p>Please enter the word you want to chage into morse code</p>
<p>
<input name="txt_msg" type="text" id="txt_msg">
</p>
<p>
<input type="submit" name="Submit" value="Do It !">
</p>
</form>';
} else {
$xmlvars='<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:s0="http://www.regom.de"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<s0:MsgtoMorse>
<s0:msg>' . $txt_msg . '</s0:msg>
</s0:MsgtoMorse>
</soap:Body>
</soap:Envelope>';
$ch=curl_init("http://helena.europe.webmatrixhosting.net/Morse.asmx");
curl_setopt($ch,CURLOPT_HTTPHEADER, Array("Content-Type: text/xml; charset=UTF-8","SOAPAction: http://www.regom.de/MsgtoMorse"));
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xmlvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($ch);
echo $result;
}
?>Regards,
Steven Freeman