XML Clients using PHP + cURL (instead of PEAR/NuSOAP) UPDATE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

XML Clients using PHP + cURL (instead of PEAR/NuSOAP) UPDATE

Post by ihateevilbill »

Hi there people,

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

?>
The second tutorial basically puts a front end on the client which allows you to input the text you want to be changed into morse code, it is as follows (comments have been removed, as it is basically the same PHP from above):

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;
}
?>
Now you wont have to trawl the internet looking for the easy way to implement clients in PHP.

Regards,

Steven Freeman
Last edited by ihateevilbill on Tue Apr 06, 2004 6:23 am, edited 2 times in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Good stuff.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Very nice work, Steven! :) Thank you!

As a general introduction into SOAP, what it is, what it stands for and how to make use of it this article is quite useful:
http://www.planetpdf.com/mainpage.asp?WebPageID=3452

:)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Thanks, Steven. I'm glad that our conversation led to something useful. Now you know about SOAPAction header, I know about it too (and know that SOAP uses POST method), and everybody know how to use cURL to query the webservices. That's the power of knowledge sharing, I'd say ;)
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

Post by ihateevilbill »

A couple of weeks ago i showed you how to connect and get a response from a soap server (morse)
Now I am going to show you how to actually transform that to something prettier (also knows as HTML ;) )

The response you should have got is as follows:

Code: Select all

&lt;soap:Envelope
	xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
	&lt;soap:Body&gt;
		&lt;MsgtoMorseResponse
			xmlns="http://www.regom.de"&gt;
			&lt;MsgtoMorseResult&gt;... - . ...- . -. &lt;/MsgtoMorseResult&gt;
		&lt;/MsgtoMorseResponse&gt;
	&lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;
and this is held in a variable called $result


What we now need is an XSLT file to convert all this SOAP rubbish into HTML
Save this file in your web root folder as morse.xsl

Code: Select all

&lt;?xml version="1.0" ?&gt;

&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.regom.de"&gt; &lt;!-- this sets up the namespace that was given to us in the soap response --&gt;

&lt;!-- The rest of this is standard xsl, for more info you can check out http://www.w3schools.com/xsl/default.asp --&gt;
&lt;xsl:template match="/"&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;span align="center"&gt;Morse code below&lt;p/&gt;
    &lt;font size="5" color="red"&gt;Red: &lt;xsl:value-of select="//m:MsgtoMorseResult"/&gt;&lt;/font&gt;&lt;p/&gt;
    &lt;font size="5" color="blue"&gt;Blue: &lt;xsl:value-of select="//m:MsgtoMorseResult"/&gt;&lt;/font&gt;&lt;p/&gt;
    &lt;font size="5" color="green"&gt;Green: &lt;xsl:value-of select="//m:MsgtoMorseResult"/&gt;&lt;/font&gt;&lt;/span&gt;
    &lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

Now all we need to do is add a couple of lines to our original PHP file to actually transform the data.
Make a copy of your old php file and edit the original so it looks like this:

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); 

// Startup Sablotron (XSLT addin for PHP)
$xh = xslt_create();
$arguments = array('/_xml' => $result);
$resultnew = xslt_process($xh, 'arg:/_xml', 'morse.xsl', NULL, $arguments);
echo $resultnew;
xslt_free($xh);

} 
?>
Now when you run this new php file (from your root folder) it will bring back the response as pretty HTML (well, not so pretty as I couldnt be bothered taking the time to do anything other than printing the response out 3 times in different colours, but you get the idea)

Again, hope this gives you better insight into the RQ->RS->XSLT process,

Steven Freeman
Last edited by ihateevilbill on Thu Apr 08, 2004 11:22 am, edited 1 time in total.
ihateevilbill
Forum Commoner
Posts: 29
Joined: Fri Nov 14, 2003 5:57 am

Post by ihateevilbill »

Forgot to mention this in my earlier posts:

You have to go into your php.ini file (%systemroot%/php.ini) and enable the following dlls (by removing the semi-colon in front of them):

extension=php_curl.dll
extension=php_openssl.dll (sometimes connecting to an https wont work on my machine without this enabled along with cURL, bit weird but better safe than confused)
extension=php_xslt.dll

These are the 3 main dlls needed when talking to and dealing with XML requests of any type and then transforming the responses.
Post Reply