📜 ⬆️ ⬇️

Working with web servers in php using SOAP

I will not dwell on the question of what web services are and why they are needed. There are a lot of articles on this topic. Just try to briefly show how it is possible to create a client for any web service in php.


Customization

To use SOAP in php, you must connect the SOAP module (included in the php5 distribution). Under windows this is done simply - it is necessary to add (to add it, since this line is not just commented out there, it is absent altogether) in php.ini:
extension = php_soap.dll
')
Do not forget to restart the server if php is installed as a module.

Creating a SOAP client using a WSDL document

The creation of a SOAP client usually follows a WSDL document, which is an XML document in a specific format that fully describes a particular web service. For details on the WSDL, I am sending you to the W3C website at www.w3.org/TR/2005/WD-wsdl20-soap11-binding-20050510 .

The most important thing to know in order to build a client to a web service is to know the URL of its WSDL document.
For example, take the “Currency Exchange Rate” web service from xmethods.com. The address of this web service that allows you to receive exchange rates online is www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl .

The second important point is that from the description of the web service you need to get information about what methods this service provides, and what parameters we should pass to it as input values ​​(very much like calling a normal php function or a class method). Usually this information is contained in the description of the service on its website. Our web service for getting currency rates is provided by the getRate () method, to which currency codes are passed as arguments.

And finally, it is important to know what to expect as an answer: how many values, what type, etc. This can also be obtained from the description.
As a result, the code is very simple and compact, almost elementary:

<?php
// Web-
// "Currency Exchange Rate" xmethods.com

// SOAP- WSDL-
$client = new SoapClient("http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl");

// c SOAP-
$result = $client->getRate("us", "russia");

echo ' : ', $result, ' ';
?>


As you can see from the code, you must pass the URL of the WSDL document to the constructor of the SoapClient class and get the object to work with the necessary web service. Then the method of this object is called, the name of which coincides with the name of the web service method itself. This method returns the desired result.

So, this simple example illustrates the principle of building a SOAP client for php web services. However, in a real application there is still much to take care of, in particular, that at the time of accessing the web service it may be temporarily unavailable or return an error. Explicitly suggests using a try / catch / throw block :)

Source: www.scriptz.com.ua/2008/04/19/rabota_s_vebserverami_na_php_posredstvom_soap.html

Source: https://habr.com/ru/post/38370/


All Articles