Php - Soap
Table of Contents
About
Articles Related
Example
$context = stream_context_create([
'ssl' => [
'local_cert' => "ws/mypem.pem", // PEM encoded file path (relative to the working directory or absolute) which contains the certificate and private key. - If Bad Path => SoapFault : Could not connect to host
'passphrase' => "secret", // If Bad => Could not connect to host
'verify_peer' => false, // True = SoapFault : Could not connect to host in 5.6 https://bugs.php.net/bug.php?id=68855
'verify_peer_name' => false
]]);
$this->_client = new SoapClient("wsdl/gdo.wsdl", // wsdl may be null (uri and location are then mandatory)
array(
// Stuff for development.
'trace' => 1,
'exceptions' => true, // Will throw an exception if the response is not ok
'cache_wsdl' => WSDL_CACHE_NONE, // Will not cache the parsing of the wsdl if any
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
// If no WSDL
'location' => $location, // SOAP URL endpoint - Must be given here and not on the SOAP call
'uri' => $uri, // Top: Namespace - Must be given here and not on the SOAP call
// Auth credentials for the SOAP request.
'login' => 'username',
'password' => 'password',
// Proxy url.
'proxy_host' => 'example.com', // Do not add the schema here (http or https). It won't work.
'proxy_port' => 44300,
// Auth credentials for the proxy.
'proxy_login' => NULL,
'proxy_password' => NULL,
// Ssl certificate
'stream_context' => $context
) );
// Description of the SOAP function from the wsdl if any
var_dump($this->_client->__getFunctions());
// Description of the SOAP data type from the wsdl if any
var_dump($this->_client->__getTypes());
// Make a call (see fault handling below)
$response = $this->_client->__soapCall(
"functionName",
array($soapVar));
var_dump($response);
// Get the request, property trace must be 1, 'trace' => 1
// Tip: You get it also normally as return value from the __soapCall function
var_dump($this->_client->__getLastRequest());
Fault handling
When instantiating the soapClient, you may give in the options array the property 'exceptions'. If it's set to:
- false: you need to test the response in order to see if it's a SoapFault object
- true: you catch the fault in the catch block.
See an example below
try {
$response = $this->_client->__soapCall(
"functionName",
array($soapVars));
// Needed if the soapClient options array has the property 'exceptions' set to false
// Otherwise you get this object in the catch block below
if (is_soap_fault($response)) {
$this->handleSoapFault($response);
}
} catch (SoapFault $e) {
// Needed if the soapClient options array has the property 'exceptions' set to true
// Otherwise you get this object back from the soapCall function
$this->handleSoapFault($e);
}
where handleSoapFault may be:
/**
* @param SoapFault $soapFault
*/
protected function handleSoapFault($soapFault)
{
echo "Soap Fault\n";
printf("Error Message : %s \n", $soapFault->getMessage()); // The faultstring node content of the response
printf("Error Code : %s \n", $soapFault->getCode()); // 0
printf("Error File : %s \n", $soapFault->getFile()); // The file itself
printf("Fault Factor : %s \n", $soapFault->faultactor);
printf("Fault Code : %s \n", $soapFault->faultcode);
printf("Fault Detail : %s \n", $soapFault->detail->{'error-message'});
}
SSL
Version:
echo (OPENSSL_VERSION_TEXT);
OpenSSL 0.9.8l 5 Nov 2009