Studyon Minte9.com
ZCE 5.3

Study

SOAP



	// XMLRPC, SOAP, REST
	// defined by WSDL (pronounced wishdl)
	// new SoapClient($wsdl) / (NULL, array(location, uri))
	// arguments (encoding)
	// debugging: getLastRequestHeader()
	// server: new SoapServer($wsdl) / handle()
	// class: setClass / autodiscovery Zend (doc /**)
	// __getFunctions / doRequest / getLastResponseHeders / setCookie



* Web Services Basics (214)

Web services are noted for being extensible and interoperable, and they are characterized by their use of 
XML to communicate between and among disparate systems.

There are three popular types of Web Services in use today: XMLRPC, SOAP, and REST. PHP 5 contains 
tools particularly suited for SOAP and RESTWeb services.


* SOAP Basics

SOAP is a powerful tool for communication between disparate systems, as it allows the definition and 
exchange of complex data types in both the request and response, as well as providing a mechanism for 
various messaging patterns, the most common of which is the Remote Procedure Call (RPC).

SOAP is intrinsically tied to XML because all messages sent to and from a SOAP server are sent in a 
SOAP envelope that is an XML wrapper for data read and generated by the SOAP server.

PHP 5 makes the creation of both servers and clients very easy.

A SOAP Web service is defined by using a Web Service Description Language (WSDL, pronounced 
"whisdl") document. This, in turn, is yet another XML document that describes the function calls made 
available by a Web service, as well as any specialized data types needed by it.



* Accessing SOAP-based Web Services (Client)

The SoapClient class provides what is essentially a one-stop solution to creating a SOAP client - all you 
really need to do is provide it with the path to a WSDL file, and it will automatically build a PHP-friendly 
interface that you can call directly from your scripts.

First, you need to activate extension in /apache/bin/php.ini ( ;extension=php_soap.dll )

<?php
	// World Championship football 2010(South Africa)
	$client = new SoapClient("http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");
	
	try {
		$topScorers = $client->TopGoalScorers(array('iTopN'=>5));
			// var_dump($topScorers);
		
		if (count($topScorers->TopGoalScorersResult->tTopGoalScorer)) {
			foreach($topScorers->TopGoalScorersResult->tTopGoalScorer as $k=>$v) {
				echo "<br>{$v->sName}: {$v->iGoals} goals";
			}
				/* Output:
				Alan Dzagoev: 3 goals
				Cristiano Ronaldo: 3 goals
				Fernando Torres: 3 goals
				Mario Balotelli: 3 goals
				Mario Gómez: 3 goals
				*/
		}
	} catch (SoapFault $e) {
		echo $e->getMessage;
	}



* Arguments

The constructor of the SOAPClient class also accepts, as an optional second parameter, an array of options 
that can alter its behaviour; for example, you can change the way data is encoded, or whether the entire 
SOAP exchange is to be compressed, and so on.

<?php
	$client = new SoapClient("http://footballpool.dataaccess.eu/data/info.wso?wsdl", ' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl", array(' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl", ' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl", array(
		'encoding'=>'ISO-8859-1',
		'connection_timeout'=>9999, 
		)); 
			// Mario Gomez: 3 goals



* No WSDL file

The WSDL is supposed to be a public document that describes the SOAP service, so describes the 
signatures of all the methods available in the service.

There may be service providers who want to expose a service to certain consumers, but who don't want to 
make the signature of the service public.

If you are accessing a SOAP service that does not have a WSDL file, it is possible to create a SOAP client 
in non-WSDL mode by passing a NULL value to the SoapClient constructor instead of the location of the 
WSDL file. In this case, you will have to pass the URI to theWeb service's entry point as part of the second 
parameter.

<?php
	$client = new SoapClient(NULL, array(
		'location' => 'http://www.nanonull.com/TimeService/TimeService.asmx',' target='_blank'>http://www.nanonull.com/TimeService/TimeService.asmx',
		'uri' => 'http://www.Nanonull.com/TimeService/',' target='_blank'>http://www.Nanonull.com/TimeService/',
	));

		/*
			http://www.nanonull.com/TimeService/TimeService.asmx
			A sample Time service
			The following operations are supported.
			    getCityTime
			    getOffesetUTCTime
			    getServerCity
			    getServerTime
			    getServerTimeZone
			    getTimeZoneTime
			    getUTCTime 
		*/

	$return = $client->__soapCall("getTimeZoneTime",
		array(new SoapParam('CET', 'ns1:timezone')),
		array('soapaction' => 'http://www.Nanonull.com/TimeService/getTimeZoneTime')' target='_blank'>http://www.Nanonull.com/TimeService/getTimeZoneTime')
	);
		// Central European Time	Europe	UTC + 2 hours
		// http://www.timeanddate.com/library/abbreviations/timezones
	
	var_dump($return);
		// Output: string(8) "11:42 AM"



* Debugging

SoapClient provides special methods thatmake it possible to debug messages sent to and received from a 
SOAP server. They can be turned on by setting the trace option to 1 when instantiating a SOAP client 
object. This, in turn, will make it possible for you to access the raw SOAP headers and envelope bodies.
	
	<?php

		$client = new SoapClient(NULL, array(
			'location' => 'http://www.nanonull.com/TimeService/TimeService.asmx',' target='_blank'>http://www.nanonull.com/TimeService/TimeService.asmx',
			'uri' => 'http://www.Nanonull.com/TimeService/',' target='_blank'>http://www.Nanonull.com/TimeService/',
			'trace' => 1,
		));
		
		$return = $client->__soapCall("getTimeZoneTime",
			array(new SoapParam('CET', 'ns1:timezone')),
			array('soapaction' => 'http://www.Nanonull.com/TimeService/getTimeZoneTime')' target='_blank'>http://www.Nanonull.com/TimeService/getTimeZoneTime')
		);

		echo $client->__getLastRequestHeaders();

			/* Outputs

				POST /TimeService/TimeService.asmx HTTP/1.1
				Host: www.nanonull.com
				Connection: Keep-Alive
				User-Agent: PHP-SOAP/5.3.16
				Content-Type: text/xml; charset=utf-8
				SOAPAction: "http://www.Nanonull.com/TimeService/getTimeZoneTime"
				Content-Length: 540
			*/

		echo $client->__getLastRequest();

			/* Outputs
			
				< xml version="1.0" encoding="UTF-8" >
				<SOAP-ENV:Envelope 
					xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
					xmlns:ns1="http://www.Nanonull.com/TimeService/" 
					xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
					xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
					xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
                                             SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
					<SOAP-ENV:Body>
						<ns1:getTimeZoneTime>
							<ns1:timezone xsi:type="xsd:string">CET</ns1:timezone>
						</ns1:getTimeZoneTime>
					</SOAP-ENV:Body>
				</SOAP-ENV:Envelope>

			*/



* Creating SOAP-based Web Services (Server)

Just as SoapClient, simplifies the task of building aWeb service client, the SoapServer class performs all of 
the background work of handling SOAP requests and responses. When creating a SOAP server, you simply 
startwith a class that contains the methods you wish tomake available to the public through a Web Service 
and use it as the basis for a SoapServer instance.

When creating a SOAP server with SoapServer, you must decide whether your server will operate in WSDL 
or non-WSDL mode. At present, SoapServer will not automatically generate a WSDL file based on an 
existing PHP class, although this feature is planned for a future release. For now, you can either create your
WSDL files manually - usually an incredibly tedious task, use a tool (like the Zend Studio IDE) that will
generate one for you, or choose not to provide one at all. For the sake of simplicity, our example SOAP 
server will operate in non-WSDL mode.

	<?php
		// http://localhost/tests/webservices/server.php (Server)

		class MySoapServer 
		{
			public function getMessage() 
			{
				return "Hello World";
			}
		}
		$server = new SoapServer(NULL, array('uri' => 'http://localhost/tests/webservices/'));

		$server->setClass('MySoapServer');
			// Once we have created the server,
			// we need to inform it of the class that we want the web service to be based on.

		$server->handle();
			// Finally, to process incoming requests, call the handle() method:

	<?php
		// http://localhost/tests/webservices/index.php (Client)

		$client = new SoapClient(NULL, array(
			'location' => 'http://localhost/tests/webservices/server.php',
			'uri' => 'http://localhost/tests/webservices/',
		));
		echo $client->getMessage();
			// Outputs: Hello World



* SoapClient Methods


	* __getFunctions() - only works in WSDL mode

	<?php
			// World Championship football 2010(South Africa)
		$client = new SoapClient("http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");
		$functions = $client->__getFunctions();
		var_dump($functions);
			/* Outputs
			array(42) {
			   [0]=>
			   string(65) "AllPlayerNamesResponse AllPlayerNames(AllPlayerNames $parameters)"
				  ...
			*/
	

	* __doRequest()

	<?php
		class MySoapClient extends SoapClient {

			function __construct($wsdl, $options = array()) {
			  parent::__construct($wsdl, $options);
			}
			
			// This method can be overridden in subclasses to implement different transport layers, 
			// perform additional XML processing or other purpose. 
			function __doRequest($request, $location, $action, $version) {
				return parent::__doRequest($request, $location, $action, $version);
			}
		}
		
		$client = new MySoapClient("http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl");
		$topScorers = $client->TopGoalScorers(array('iTopN'=>5));
		

	* __getLastResponseHeaders() - only works with trace = 1

		<?php
			$client = new SoapClient("http://footballpool.dataaccess.eu/data/info.wso?wsdl", ' target='_blank'>http://footballpool.dataaccess.eu/data/info.wso?wsdl", 
						array('trace'=>1));

			$client->TopGoalScorers(array('iTopN'=>5));
				var_dump($client->__getLastResponseHeaders ());
				/* Outputs:
					HTTP/1.1 200 OK
					Cache-Control: private, max-age=0
					Content-Length: 2094
					Content-Type: text/xml; charset=utf-8
					Server: Microsoft-IIS/7.5
					Web-Service: Visual DataFlex 16.1
					Date: Fri, 02 Nov 2012 12:46:39 GMT
				*/


	* __setCookie()

		<?php
			$client = new SoapClient("http://footballpool.dataaccess.eu/data/info.wso?wsdl",
						 array('trace'=>1));

			$client->__setCookie("myname", "catalin" );
			$client->TopGoalScorers(array('iTopN'=>5));
				var_dump($client->__getLastRequestHeaders());
				/* Outputs:
					...
					Cookie: myname=catalin;
				*/



Testare !!

http://www.codeproject.com/Articles/41844/Webservice-Client-and-Server-demo-in-PHP


$client = new SoapClient("http://web.test.infrontservices.com/PartnerService/BrdService.svc?singleWsdl", array(

                                                'proxy_host'     => "isabrd", //"172.17.50.9",

                                                'proxy_port'     => 8080,

                                                'proxy_login' => "BRDcatalin.prescure",

                                                'proxy_password' => "XXX",

                                ));

$auth = array('Code'=>'4a182071-8dc9-4127-a972-acdf84ba79d1');

$result= $client->call('register', $auth);

                print_r($result);


http://www.vankouteren.eu/blog/2009/03/simple-php-soap-example
http://stackoverflow.com/questions/928169/php-soap-non-wsdl-call-how-do-you-pass-parameters