MemoryRefresh!

Various / SOAP   SOAP



Questions Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


Web Services Basics
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 esponse. 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("footballpool.dataaccess.eu"); 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("footballpool.dataaccess.eu", 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' => 'nanonull.com', 'uri' => 'www.', )); /* nanonull.com 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' => 'www.') ); // Central European Time Europe UTC + 2 hours // timeanddate.com 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' => 'nanonull.com', 'uri' => 'www.', 'trace' => 1, )); $return = $client->__soapCall("getTimeZoneTime", array(new SoapParam('CET', 'ns1:timezone')), array('soapaction' => 'www.') ); 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: "www." Content-Length: 540 */ echo $client->__getLastRequest(); /* Outputs < xml version="1.0" encoding="UTF-8" > <SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap.org" xmlns:ns1="www." xmlns:xsd="w" xmlns:xsi="w" xmlns:SOAP-ENC="schemas.xmlsoap.org" SOAP-ENV:encodingStyle="schemas.xmlsoap.org"> <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 // localhost (Server) class MySoapServer { public function getMessage() { return "Hello World"; } } $server = new SoapServer(NULL, array('uri' => 'localhost')); $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 // localhost (Client) $client = new SoapClient(NULL, array( 'location' => 'localhost', 'uri' => 'localhost', )); echo $client->getMessage(); // Outputs: Hello World SoapClient Methods __getFunctions() - only works in WSDL mode
<?php // World Championship football 2010(South Africa) $client = new SoapClient("footballpool.dataaccess.eu"); $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("footballpool.dataaccess.eu"); $topScorers = $client->TopGoalScorers(array('iTopN'=>5)); __getLastResponseHeaders() - only works with trace = 1
<?php $client = new SoapClient("footballpool.dataaccess.eu", 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("footballpool.dataaccess.eu", array('trace'=>1)); $client->__setCookie("myname", "catalin" ); $client->TopGoalScorers(array('iTopN'=>5)); var_dump($client->__getLastRequestHeaders()); /* Outputs: ... Cookie: myname=catalin; */ codeproject.com vankouteren.eu stackoverflow.com

Related Pages

SPL
XML
SOAP
JSON
PHPUNIT
PHAR


0% 100%
 
 
0 pages 91 pages
1) Which web services are supported natively in PHP?





2) Can JSON represent a resource type?







Web Services Basics
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 esponse. 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("footballpool.dataaccess.eu"); 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("footballpool.dataaccess.eu", 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' => 'nanonull.com', 'uri' => 'www.', )); /* nanonull.com 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' => 'www.') ); // Central European Time Europe UTC + 2 hours // timeanddate.com 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' => 'nanonull.com', 'uri' => 'www.', 'trace' => 1, )); $return = $client->__soapCall("getTimeZoneTime", array(new SoapParam('CET', 'ns1:timezone')), array('soapaction' => 'www.') ); 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: "www." Content-Length: 540 */ echo $client->__getLastRequest(); /* Outputs < xml version="1.0" encoding="UTF-8" > <SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap.org" xmlns:ns1="www." xmlns:xsd="w" xmlns:xsi="w" xmlns:SOAP-ENC="schemas.xmlsoap.org" SOAP-ENV:encodingStyle="schemas.xmlsoap.org"> <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 // localhost (Server) class MySoapServer { public function getMessage() { return "Hello World"; } } $server = new SoapServer(NULL, array('uri' => 'localhost')); $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 // localhost (Client) $client = new SoapClient(NULL, array( 'location' => 'localhost', 'uri' => 'localhost', )); echo $client->getMessage(); // Outputs: Hello World SoapClient Methods __getFunctions() - only works in WSDL mode
<?php // World Championship football 2010(South Africa) $client = new SoapClient("footballpool.dataaccess.eu"); $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("footballpool.dataaccess.eu"); $topScorers = $client->TopGoalScorers(array('iTopN'=>5)); __getLastResponseHeaders() - only works with trace = 1
<?php $client = new SoapClient("footballpool.dataaccess.eu", 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("footballpool.dataaccess.eu", array('trace'=>1)); $client->__setCookie("myname", "catalin" ); $client->TopGoalScorers(array('iTopN'=>5)); var_dump($client->__getLastRequestHeaders()); /* Outputs: ... Cookie: myname=catalin; */ codeproject.com vankouteren.eu stackoverflow.com

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References