HomePHP Page 4 - Using XPath, SOAP, and More with Web Services
PHP’s SOAP Extension - PHP
In this fourth part of a five-part series on Web Services, you'll learn about SimpleXML objects, SOAP, and more. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
In response to the community clamor for Web Services–enabled applications, and the popularity of third-party SOAP extensions, a native SOAP extension was available as of PHP 5, and enabled by default as of PHP 6.0. This section introduces this object-oriented extension and shows you how to create both a SOAP client and server. Along the way you’ll learn more about many of the functions and methods available through this extension. Before you can follow along with the accompanying examples, you need to take care of a few prerequisites, which are discussed next.
PHP’s SOAP extension requires the GNOME XML library. You can download the latest stablelibxml2package fromhttp://www.xmlsoft.org/. Binaries are also available for the Windows platform. Version 2.5.4 or greater is required. If you’re running a version of PHP older than 6.0, you’ll also need to configure PHP with the--enable-soapextension. On Windows, you’ll need to add the following line to yourphp.inifile:
Instantiating the Client
The SoapClient() constructor instantiates a new instance of the SoapClient class. The prototype looks like this:
Thewsdlparameter determines whether the class will be invoked in WSDL or non-WSDL mode; if in WSDL mode, set it to the WSDL file URI, otherwise set it toNULL. Theoptionsparameter is an array that accepts the following parameters. It’s optional for WSDL mode and requires that at least thelocationandurl options are set when in non-WSDL mode:
actor: This parameter specifies the name, in URI format, of the role that a SOAP node must play in order to process the header.
compression: This parameter specifies whether data compression is enabled. Presently, Gzip and x-gzip are supported. According to the TODO document, support is planned for HTTP compression.
exceptions: This parameter turns on the exception-handling mechanism. It is enabled by default.
location: This parameter is used to specify the endpoint URL, when working in non-WSDL mode.
login: This parameter specifies the username if HTTP authentication is used to access the SOAP server.
password: This parameter specifies the password if HTTP authentication is used to access the SOAP server.
proxy_host: This parameter specifies the name of the proxy host when connecting through a proxy server.
proxy_login: This parameter specifies the proxy server username if one is required.
proxy_password: This parameter specifies the proxy server password if one is required.
proxy_port: This parameter specifies the proxy server port when connecting through a proxy server.
soap_version: This parameter specifies whether SOAP version 1.1 or 1.2 should be used. This defaults to version 1.1.
trace: This parameter specifies whether you’d like to examine SOAP request and response envelopes. If so, you’ll need to enable this by setting it to 1.
uri: This parameter specifies the SOAP service namespace when not working in WSDL mode.
Establishing a connection to a Web Service is trivial. The following example shows you how to use theSoapClientobject to connect to a sports-related Web service I’ve created to retrieve a random boxing quote:
However, just referencing the Web Service really doesn’t do you much good. You’ll want to learn more about the methods exposed by this Web Service. Of course, you can open up the WSDL document in the browser or a WSDL viewer by navigating tohttp://www.wjgilmore.com/boxing.wsdl. However, you can also retrieve the methods programmatically using the__getFunctions()method, introduced next.
Please check back for the conclusion to this series.