MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP SOAP Client

WSDL Mode


First, create a new SoapClient object, passing the URL to the WSDL file and optionally, an array of options.
// Create a new client object using a WSDL URL
$soap = new SoapClient('https://example.com/soap.wsdl', [
 # This array and its values are optional
 'soap_version' => SOAP_1_2,
 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
 'cache_wsdl' => WSDL_CACHE_BOTH,
 # Helps with debugging
 'trace' => TRUE,
 'exceptions' => TRUE
]);

Then use the $soap object to call your SOAP methods.
$result = $soap->requestData(['a', 'b', 'c']);

Non-WSDL Mode


This is similar to WSDL mode, except we pass NULL as the WSDL file and make sure to set the location and uri options.
$soap = new SoapClient(NULL, [
 'location' => 'https://example.com/soap/endpoint',
 'uri' => 'namespace'
]);

Classmaps


When creating a SOAP Client in PHP, you can also set a classmap key in the configuration array. This classmap defines which types defined in the WSDL should be mapped to actual classes, instead of the default StdClass. The reason you would want to do this is because you can get auto-completion of fields and method calls on these classes, instead of having to guess which fields are set on the regular StdClass.
class MyAddress {
 public $country;
 public $city;
 public $full_name;
 public $postal_code; // or zip_code
 public $house_number;
}
class MyBook {
 public $name;
 public $author;
 // The classmap also allows us to add useful functions to the objects
 // that are returned from the SOAP operations.
 public function getShortDescription() {
 return "{$this->name}, written by {$this->author}";
 }
}
$soap_client = new SoapClient($link_to_wsdl, [
 // Other parameters
 "classmap" => [
 "Address" => MyAddress::class, // ::class simple returns class as string
 "Book" => MyBook::class,
 ]
]);

After configuring the classmap, whenever you perform a certain operation that returns a type Address or Book, the SoapClient will instantiate that class, fill the fields with the data and return it from the operation call.
// Lets assume 'getAddress(1234)' returns an Address by ID in the database
$address = $soap_client->getAddress(1234);

// $address is now of type MyAddress due to the classmap
echo $address->country;

// Lets assume the same for 'getBook(1234)'
$book = $soap_client->getBook(124);

// We can not use other functions defined on the MyBook class
echo $book->getShortDescription();

// Any type defined in the WSDL that is not defined in the classmap
// will become a regular StdClass object
$author = $soap_client->getAuthor(1234);

// No classmap for Author type, $author is regular StdClass.
// We can still access fields, but no auto-completion and no custom functions
// to define for the objects.
echo $author->name;

Tracing SOAP request and response


Sometimes we want to look at what is sent and received in the SOAP request. The following methods will return the XML in the request and response:
SoapClient::__getLastRequest()
SoapClient::__getLastRequestHeaders()
SoapClient::__getLastResponse()
SoapClient::__getLastResponseHeaders()

For example, suppose we have an ENVIRONMENT constant and when this constant's value is set to DEVELOPMENT we want to echo all information when the call to getAddress throws an error. One solution could be:
try {
 $address = $soap_client->getAddress(1234);
} catch (SoapFault $e) {
 if (ENVIRONMENT === 'DEVELOPMENT') {
 var_dump(
 $soap_client->__getLastRequestHeaders()
 $soap_client->__getLastRequest(),
GoalKicker.com – PHP Notes for Professionals 189
 $soap_client->__getLastResponseHeaders(),
 $soap_client->__getLastResponse()
 );
 }
 ...
}


Conclusion

In this page (written and validated by ) you learned about PHP SOAP Client . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP cURL.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.