Zend PHP 7 Certification – Data Formats and Types – SOAP

This post covers the SOAP section of the Data Formats and Types chapter when studying for the Zend PHP 7 Certification.

The SOAP (Simple Object Access Protocol) extension can be used to write SOAP Servers and Clients. SOAP requires the libxml extension enabled (it usually is by default).

You can instantiate a new SoapClient class using the new keyword, and passing in the WSDL as an argument.

$client = new SoapClient("http://example.com/webservices?wsdl");

So what is WSDL? WSDL is a service description format used by SOAP to describe the input and output of the server and what methods exist. WSDL uses XML to describe the input/output messages. The format is pretty complex, however a lot of programming languages provide the ability to auto-generate WSDLs from code.

WSDL cache functions are affected by configuration directives set in php.ini. To enable WSDL cache, set the soap.wsdl_cache_enabled directive to 1.

WSDL caching may be disabled by the use of the soap.wsdl_cache_ttl configuration directive.

The SoapClient class can actually pass in two arguments: the WSDL URL and an array of options.

The majority of SOAP clients require some form of HTTP authentication to communicate with the API. SOAP gives you the ability to pass in a login and a password in the form of an array of options.

<?php
options = array(
    'login' => $username,
    'password' => $password,
);
$client = new SoapClient($wsdl, $options);

When you have been successfully been authenticated, you can use the __soapCall() method to call functions defined in the WSDL document.

__soapCall() takes two parameters: the function name as a string and an array of parameters.

<?php
$client = new SoapClient($wsdl, $options);

// Set parameters for the request
$params = array(
  // Some params
);

// Invoke web service method
$response = $client->__soapCall("someFunction", array($params));

// Print web service response
var_dump($response);

PHP provides a couple of in-built functions to handle SOAP errors.

is_soap_fault — This function checks if a SOAP call has failed.

<?php
$client = new SoapClient("some.wsdl", array('exceptions' => 0));
$result = $client->SomeFunction();
if (is_soap_fault($result)) {
    trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}

use_soap_error_handler — This function sets whether or not to use the SOAP error handler in the SOAP server.

<?php
use_soap_error_handler(true);

The SoapServer class is similar to SoapClient in that it is instantiated passing in the WSDL.

$server = new SoapServer("somewsdl.wsdl");

You can add one of more functions to handle SOAP requests using the addFunction() method of the SoapServer class.

<?php
$server = new SOAPServer("somewsdl.wsdl");

function echoString($inputString)
{
    return $inputString;
}

$server->addFunction("echoString");

View the other sections:

Note: This article is based on PHP version 7.0.