SOAP (Simple Object Access Protocol) представляет из себя основанный на XML протокол, предназначенный для обмена структурированной информацией между распределенными приложениями поверх существующих в веб протоколов, например HTTP. Спецификация SOAP определяет формат, используемый XML-сообщениями, то, как они должны обрабатываться набор правил кодирования для стандарта, типы данных а также соглашения для вызова удаленных процедур и ответы на вызовы.
Веб-сервисы - это модная и современная технология. Список технологий, относящихся к веб-сервисам увеличивается практически ежедневно, но SOAP является, вероятно, наиболее важной из них. Он стремительно становится стандартным протоколом доступа к веб-сервисам. Он использует XML-сообщения для обмена информацией между конечными точками, и в тоже время предоставляет некоторые преимущества бинарных протоколов. Поддержка RPC (Remote Procedure Calls) в начале была одной из незначительных возможностей протокола SOAP, но сейчас она превратилась в одну из наиболее часто используемых возможностей.
SOAP-расширение для PHP 5 - это первая попытка организовать поддержку SOAP в PHP на Си. У нее есть несколько преимуществ перед существующими реализациями SOAP, написанными на PHP, и самое важное из них - скорость. В данный момент расширение считается экспериментальным, но постепенно оно будет становиться все боле надежным и стабильным.
В расширении SOAP реализованы большие подмножества спецификаций SOAP 1.1, SOAP 1.2 и WSDL 1.1. Главная цель - максимально использовать RPC-возможности SOAP. Везде, где это возможно используется WSDL, чтобы сделать реализацию веб-сервисов более простой.
Первый SOAP-клиент
Чтобы продемонстрировать создание простого SOAP-клиента используем демонстрационный сервис "Delayed Stock Quote" с сайта XMethods. Перед тем как мы начнем писать PHP-код, необходимо собрать следующую информацию о данном конкретном сервисе:
- Имя метода
- URL по которому расположен этот сервис
- Значение заголовка SOAPAction метода
- Пространство имен метода
- Имена и типы входных и выходных параметров метода
К счастью, вся эта информация доступна на сайте XMethods по адресу http://www.xmethods.com/ из RPC-профиля сервиса:
Имя метода | getQuote |
URL сервиса | http://66.28.98.121:9090/soap |
SOAPAction | urn:xmethods-delayed-quotes#getQuote |
Пространство имен метода | urn:xmethods-delayed-quotes |
Входные параметры | Symbol (String) |
Выходные параметры | Result (float) |
Пример 1 (client1.php)
$client
= new
SoapClient
(NULL
,
array(
"location"
=>
"http://66.28.98.121:9090/soap"
,
"uri"
=>
"style"
=>
SOAP_RPC
,
"use"
=>
SOAP_ENCODED
));
Print($client
->
__call
(
/* Имя SOAP-метода */
"getQuote"
,
/* Параметры */
array(
new
SoapParam
(
/* Значение параметра */
"ibm"
,
/* Имя параметра */
"symbol"
)),
/* Опции */
array(
/* Пространство имен SOAP-метода */
"uri"
=>
"urn:xmethods-delayed-quotes"
,
/* HTTP-заголовок
SOAPAction для SOAP-метода */
"soapaction"
=>
"urn:xmethods-delayed-quotes#getQuote"
)).
"\n"
);
?>
Как видите, решение этой простой задачи потребовало довольно много работы.
К счастью веб-сервисы могут описывать себя клиентам с помощью WSDL, в целом это довольно удобно. WSDL для сервиса "Delayed Stock Quote" представлен на его информационной страничке на сайте xmethods.com - http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl .
Вот вариант этого же клиента, переписанный для работы с помощью этого WSDL-документа. Здесь нам уже не нужно указывать URI-сервера, пространство имен, заголовок SOAPAction, способ кодирования и типы параметров. Вся эта информация берется из WSDL файла.
Пример 2 (client2.php)
$client
= new
SoapClient
(
"http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl"
);
Print($client
->
getQuote
("ibm"
));
?>
Так несколько проще, правда?
Какие проблемы возникают при использовании WSDL? Единственный аргумент против его использования состоит в том, что клиент должен прочитать WSDL с сервера до того, как можно будет вызвать какую-нибудь процедуру, а в веб это может занять довольно много времени. Для того, чтобы ускорить работу в SOAP-расширении предусмотрены следующие параметры конфигурации: soap.wsdl_cache_enabled, soap.wsdl_cache_dir and soap.wsdl_cache_ttl. Их можно задать в файле php.ini или с помощью ini_set()(см. Пример 4). По умолчанию кэширование WSDL включено и WSDL-файлы кэшируются на 1 день.
Вот раздел SOAP файла php.ini с значениями по умолчанию. Вы можете скопировать их в свой php.ini.
[ soap ]
Soap
.
wsdl_cache_enabled
=
"1"
;
включает или выключает кэширование WSDL
Soap
.
wsdl_cache_dir
=
"/tmp"
;
задает имя директории в которой
SOAP
-
расширение будет хранить кэшированные
файлы
Soap
.
wsdl_cache_ttl
=
"86400"
; (время жизни
)
устанавливает время
(в
секундах
)
которое файлы из кэша могут
использоваться
Первый SOAP-сервер
Попробуем написать собственный SOAP веб-сервис, который будет делать тоже, что и сервис "Delayed Stock Quote" с XMethods.
Первое, что нужно сделать - это создать WSDL-документ, описывающая наш сервис в формате, понятном клиентам. Для этого понадобится несколько изменить взятый с сайта Xmethods оригинальный документ, потому начнем мы с того, что рассмотрим его более подробно.
Раздел message определяет два сообщения. Первое - getQuoteRequest, которое передает сообщение getQuote и принимает одностроковый параметр с именем symbol. Второе сообщение - getQuoteResponse, ответ на запрос getQuote, передающий одно значение типа float с именем Result.
Раздел portType определяет единственную операцию getQuote, которая указывает, какое из описанных в разделе message будет использоваться для запроса, а какое для ответа.
В разделе binding определяется как сообщение должно кодироваться и передаваться. В данном случае в нем указано, что мы пошлем RPC-запрос через HTTP, используя SOAP-кодирование. Здесь также определены пространство имен и значение заголовка SOAPAction для метода getQuote.
И наконец в разделе service определяется URL по которому находится сервис.
Пример 3 (stockquote.wsdl)
xmlns:tns=" http://example.org/StockQuote "
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
Замечание: по умолчанию кэширование WSDL включено. На время разработки и отладки вашего WSDL, кэширование лучше отключить.
Теперь самое время приступить к созданию нашего сервера.
Прежде всего мы разработаем функцию getQuote(), которая будет обрабатывать входящие запросы из веб. Далее мы создадим объект класса SoapServer и присоединим к нему нашу функцию с помощью метода SoapServer::addFunction(). Как вы увидите в дальнейшем, у конструктора SoapServer() есть только один параметр - путь к WSDL-документу, описывающему сервис.
Пример 4 (server1. php)
$quotes
= array(
"ibm"
=>
98.42
);
global
$quotes
;
return
$quotes
[
$symbol
];
}
Ini_set
("soap.wsdl_cache_enabled"
,
"0"
);
// отключаем кэширование WSDL
$server
= new
SoapServer
("stockquote1.wsdl"
);
$server
->
addFunction
("getQuote"
);
$server
->
handle
();
?>
SoapServer может работать и без WSDL, примерно также как клиент, но у такого варианта нет никаких преимуществ, из-за которых стоило бы его использовать. Если вы все же хотите работать именно так, вы должны убедиться что возвращаемые значения - это объекты классов SoapParam и SoapVar(как в первом примере.
Вот клиент для доступа к нашему SOAP-серверу. По сравнению с предыдущим примером добавилась только ссылка на местонахождение WSDL. Предполагается, что файл "stockquote1.wsdl" лежит в той же директории что и SOAP-сервер.
Пример 5 (client3.php)
$client
= new
SoapClient
("stockquote1.wsdl"
);
print($client
->
getQuote
("ibm"
));
?>
Какие основные проблемы есть в наших клиенте и сервере?
Для начала, они не обрабатывают ошибки. Что происходит, когда сервер не находит подходящий результат для переданного ему значения переменной symbol? В SOAP существует специальный формат сообщений для сообщений об ошибках - SoapFault.Чтобы сгенерировать такое сообщение, сервер должен вызвать исключение с помощью объекта SoapFault. Первый параметр конструктора SoapFault() это строка с кодом ошибки, второй - строка с описанием ошибки. Клиент должен быть написан так, чтобы обрабатывать исключения SoapFault.
Во вторых, функциональность веб-сервиса лучше инкапсулировать в PHP-класс. В этом случае нам не нужно будет использовать глобальные переменные и добавлять к серверу каждый SOAP-метод по отдельности. Вместо этого мы сможем добавить класс целиком, и все его методы станут доступны через SOAP. Вот соответствующим образом модифицированные версии клиента и сервера.
Пример 6 (server2.php)
class
QuoteService
{
private
$quotes
= array("ibm"
=>
98.42
);
Function
getQuote
($symbol
) {
if
(isset($this
->
quotes
[
$symbol
])) {
return
$this
->
quotes
[
$symbol
];
} else {
throw new
SoapFault
("Server"
,
"Unknown Symbol "$symbol"."
);
}
}
}
$server
= new
SoapServer
("stockquote2.wsdl"
);
$server
->
setClass
("QuoteService"
);
$server
->
handle
();
?>
Как видите, я использовал метод SoapServer::setClass() для соединения объекта SoapServer с классом QuoteService.
Пример 7 (client4.php)
$client
= new
SoapClient
("stockquote2.wsdl"
);
try {
echo
"
\n" ;\n" ;
print($client -> getQuote ("ibm" ));
echo "\n" ;
print($client -> getQuote ("microsoft" ));
echo "\n
} catch (SoapFault $exception ) {
echo $exception ;
}
?>
А что внутри?
Если вы хотите разобраться в формате SOAPсообщений, или хотите самостоятельно отлаживать SOAP-клиента, то этот раздел для вас.
Как вы видели в первом примере, конструктор SoapClient() принимает ассоциативный массив в качестве второго параметра. С помощью этого массива мы можем вызывать различные опции на сервере.
Посмотрим две из них:
- trace - позволяет клиенту сохранять SOAP-запросы и ответы (по умолчанию выключено).
- exceptions - позволяет клиенту контролировать механизм исключений (по умолчанию включено).
Посмотрим на следующий пример SOAP-клиента. Это доработанный клиент из Примера 5, в точности показывающий, что передается между клиентом и сервером. Для получения этой информации используются методы __getLastRequest() и __getLastResponse().
Пример 8 (client5.php)
$client
= new
SoapClient
("stockquote1.wsdl"
,array(
"trace"
=>
1
,
"exceptions"
=>
0
));
$client
->
getQuote
("ibm"
);
print
"
\n" ;" ;
print "Запрос:\n" . htmlspecialchars ($client -> __getLastRequest ()) . "\n" ;
print "Ответ:\n" . htmlspecialchars ($client -> __getLastResponse ()). "\n" ;
print "
?>
Вот вывод скрипта. Он немного изменен, для упрощения понимания.
Запрос:
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
Ответ:
xmlns:ns1="urn:xmethods-delayed-quotes"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
Другие реализации SOAP для PHP
Все они написаны на PHP, а не на Си.
Резюме
В этой статье я описал только основные функции SOAP-расширения. На самом деле оно может гораздо больше, но продемонстрировать все его возможности в рамках одной короткой статьи попросту невозможно. Вот список главных из них:
- Поддержка комплексных типов данных (массивов, объектов)
- Поддержка SOAP - заголовков
- Динамическая поддержка SOAP 1.1 и SOAP 1.2
Возможно они будут более подробно рассмотрены в последующих статьях.
Подробная документация по SOAP-расширению расположена по адресу http://www.php.net/manual/en/ref.soap.php .
Разработка этого расширения находится на начальном этапе, поэтому ваши отзывы помогут сделать его более стабильным, надежным, удобным и быстрым. Пожалуйста сообщайте о всех возникающих при его использовании проблемах по адресу http://bugs.php.net/ .
Ссылки
Об авторе
Я привык писать PHP-код, но не часто использую объектно-ориентированное кодирование. Теперь мне нужно взаимодействовать с SOAP (как клиент), и я не могу правильно получить синтаксис. У меня есть файл WSDL, который позволяет мне правильно настроить новое соединение с использованием класса SoapClient. Тем не менее, я не могу сделать правильный звонок и вернуть данные. Мне нужно отправить следующие (упрощенные) данные:
- Contact ID
- Контактное имя
- Общее описание
- Сумма
В документе WSDL есть две функции, но мне нужен только один ("FirstFunction" ниже). Вот script, который я запускаю, чтобы получить информацию о доступных функциях и типах:
$client = new SoapClient("http://example.com/webservices?wsdl"); var_dump($client->__getFunctions()); var_dump($client->__getTypes());
И вот результат, который он генерирует:
Array( => "FirstFunction Function1(FirstFunction $parameters)", => "SecondFunction Function2(SecondFunction $parameters)",); array( => struct Contact { id id; name name; } => string "string description" => string "int amount" }
Предположим, что я хочу позвонить FirstFunction с данными:
- Contact ID: 100
- Контактное лицо: John
- Общее описание: Barrel of Oil
- Сумма: 500
Каким будет правильный синтаксис? Я пробовал всевозможные варианты, но кажется, что мыльная структура довольно гибкая, поэтому есть очень много способов сделать это. Не удалось понять это из руководства...
ОБНОВЛЕНИЕ 1: пробованный образец из MMK:
$client = new SoapClient("http://example.com/webservices?wsdl"); $params = array("id" => 100, "name" => "John", "description" => "Barrel of Oil", "amount" => 500,); $response = $client->__soapCall("Function1", array($params));
Но я получаю этот ответ: Object has no "Contact" property . Как вы можете видеть на выходе getTypes() , существует struct , называемый Contact , поэтому, я думаю, мне почему-то нужно уточнить, что мои параметры включают данные Contact, но вопрос в следующем: как?
ОБНОВЛЕНИЕ 2: Я также пробовал эти структуры, такую же ошибку.
$params = array(array("id" => 100, "name" => "John",), "Barrel of Oil", 500,);
Также как:
$params = array("Contact" => array("id" => 100, "name" => "John",), "description" => "Barrel of Oil", "amount" => 500,);
Ошибка в обоих случаях: объект не имеет свойства "Контакт"
8 ответов
Это то, что вам нужно сделать.
Просто чтобы знать, я попытался воссоздать вашу ситуацию...
- В этом примере я создал веб-сервис.NET с помощью WebMethod под названием Function1 , и это параметры:
Функция1 (контактный контакт, описание строки, количество int)
В котором Contact есть только bean class , который имеет getters и seters для id и name , как в вашем случае.
Вы можете загрузить этот веб-сервис.NET с помощью:
Код.
Это то, что вам нужно сделать на стороне PHP :
(Протестировано и работает)
id = $id; $this->name = $name; } } /* Initialize webservice with your WSDL */ $client = new SoapClient("http://localhost:10139/Service1.asmx?wsdl"); /* Fill your Contact Object */ $contact = new Contact(100, "John"); /* Set your parameters for the request */ $params = array("Contact" => $contact, "description" => "Barrel of Oil", "amount" => 500,); /* Invoke webservice method with your parameters, in this case: Function1 */ $response = $client->__soapCall("Function1", array($params)); /* Print webservice response */ var_dump($response); ?>
Как я знаю, что это работает?
- Если вы сделаете print_r($params); , вы увидите этот вывод, поскольку ваш веб-сервис ожидает:
Массив ([Контакт] = > Контактный объект ( = > 100 = > John) [описание] = > Бочка с маслом [количество] = > 500)
- Когда я отлаживал образец.NET webservice, я получил следующее:
(Как вы видите, Contact объект не является нулевым, а также другими параметрами, это означает, что ваш запрос был успешно выполнен с PHP-стороны).
- Ответ от.NET webservice был ожидаемым и показан на стороне PHP:
object (stdClass) public "Function1Result" = > string "Подробный информация по вашему запросу! id: 100, имя: John, описание: Barrel масла, количество: 500 "(длина = 98)
Надеюсь, что это поможет: -)
Вы также можете использовать SOAP-сервисы:
"Spain", "CityName" => "Alicante"); $response = $soapclient->getWeather($params); var_dump($response); // Get the Cities By Country $param = array("CountryName" => "Spain"); $response = $soapclient->getCitiesByCountry($param); var_dump($response);
Это пример с реальным сервисом, и он работает.
Надеюсь, что это поможет.
Сначала выполните инициализацию веб-сервисов:
$client = new SoapClient("http://example.com/webservices?wsdl");
Затем установите и передайте параметры:
$params = array ("arg0" => $contactid, "arg1" => $desc, "arg2" => $contactname); $response = $client->__soapCall("methodname", array($params));
Обратите внимание, что имя метода доступно в WSDL в качестве имени операции, например:
Я не знаю, почему мой веб-сервис имеет одинаковую структуру с вами, но ему не нужен класс для параметра, просто массив.
Например: - Мой WSDL:
Var_dump($client->getFunctions()); var_dump($client->getTypes());
Вот результат:
Array 0 => string "OrderConfirmation createOrder(OrderRequest $createOrder)" (length=56) array 0 => string "struct OrderRequest { Identification identification; Delivery delivery; Parcel parcel; Receiver receiver; string reference; }" (length=130) 1 => string "struct Identification { string sender; string hash; string originator; }" (length=75) 2 => string "struct Delivery { Node from; Node to; }" (length=41) 3 => string "struct Node { string country; string node; }" (length=46) 4 => string "struct Parcel { string description; decimal weight; string orderNumber; date orderDate; }" (length=93) 5 => string "struct Receiver { string firstName; string surname; Address address; string email; string language; }" (length=106) 6 => string "struct Address { string line1; string line2; string postalCode; string city; string country; }" (length=99) 7 => string "struct OrderConfirmation { string trackingNumber; string reference; }" (length=71) 8 => string "struct OrderServiceException { string code; OrderServiceException faultInfo; string message; }" (length=97)
Итак, в моем коде:
$client = new SoapClient("http://packandship-ws.kiala.com/psws/order?wsdl"); $params = array("reference" => $orderId, "identification" => array("sender" => param("kiala", "sender_id"), "hash" => hash("sha512", $orderId . param("kiala", "sender_id") . param("kiala", "password")), "originator" => null,), "delivery" => array("from" => array("country" => "es", "node" => "",), "to" => array("country" => "es", "node" => "0299"),), "parcel" => array("description" => "Description", "weight" => 0.200, "orderNumber" => $orderId, "orderDate" => date("Y-m-d")), "receiver" => array("firstName" => "Customer First Name", "surname" => "Customer Sur Name", "address" => array("line1" => "Line 1 Adress", "line2" => "Line 2 Adress", "postalCode" => 28006, "city" => "Madrid", "country" => "es",), "email" => "[email protected]", "language" => "es")); $result = $client->createOrder($params); var_dump($result);
но он успешно!
Это хороший пример для функции SOAP "__call". Однако он устарел.
Envio Internacional: "; $vem = $cliente->__call("CustoEMSInternacional",array($int_zona, $int_peso)); print $vem; print "
"; ?>In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is a technology that uses a protocol (SOAP in this example) for exchanging data between applications over the network.
If we are developing a client for consuming a web service, we have to look at the protocol that the service is using. This example is for looking at the resources provided by PHP for developing a client for a web service implementing the SOAP protocol.
For this example, we will use:
- Ubuntu (14.04) as Operating System.
- Apache HTTP server (2.4.7).
- PHP (5.5.9).
1. Preparing the environment
1.1. Installation
Below, commands to install Apache and PHP are shown:
Sudo apt-get update sudo apt-get install apache2 php5 libapache2-mod-php5 sudo service apache2 restart
1.2. PHP configuration
Even if it’s not necessary, is recommendable to disable the SOAP WSDL cache for development environments. In /etc/php5/apache2/php.ini file, set the soap.wsdl_cache_enabled directive to 0:
php.ini
Soap.wsdl_cache_enabled=0
Don’t forget to restart Apache after doing any change.
Note: if you want to use Windows, installing XAMPP is the fastest and easiest way to install a complete web server that meets the prerequisites.
2. What is SOAP?
SOAP (Simple Object Access Protocol) is a standard protocol that defines how two different objects in different processes can communicate each other, through data exchange in XML format. Is one of the widest protocols used in web services.
In other words, this protocol allows to call method of objects that are defined in remote machines.
3. PHP example of SOAP clients
The server offering their SOAP services can define them in two different ways:
- With WSDL (Web Services Definition Language)
- Without WSDL
From the client point of view, there are very few differences, but let’s see how to proceed for each one.
We will consider the following scenario, which is quite typical: a web service that inserts and reads from a database (simulated with a plain text file), for which we will develop a client.
Is responsibility of the server offering the service to let know the protocol used, as same as the available method definitions, in order to let the clients know how to deal with the service.
Even if this tutorial is about the client itself, we will see also the server side, in order to test that the client is actually working.
3.1. Working directory structure
For this example, we will use the following structure:
Php_soapclient_example/ ├── simple_soap_client_class.php ├── simple_soap_server_class.php ├── handle_soap_request.php ├── no_wsdl │ └── server_endpoint.php └── wsdl ├── server_endpoint.php └── simple_service_definition.wsdl
Where the root directory, php_soapclient_example , will be in the web server’s root directory, which is /var/www/html/ by default.
Briefly explained each file:
- simple_soap_client_class.php is the class defined for the SOAP client methods.
- simple_soap_server_class.php is the class defined for the SOAP server methods.
- handle_soap_request.php is for instantiate and use the SimpleSoapClient class defined in simple_soap_client_class.php .
- In the directories wsdl and no_wsdl , the service defined in simple_soap_server_class.php is offered, in the way it accords to each of the modes. We won’t go into details for these, since it is not the purpose of this example. The only thing we have to know is that in one of the directories there is a service offered in WSDL mode, and that in the other, in no WSDL mode; they are two independent web services that use the code defined in simple_soap_server_class.php .
Note: the WSDL mode needs a .wsdl file, where the web service is defined. We are not going to see it in this example, since the aim is not to see how to build a WSDL definition file, but it’s also available to download in the last section of the example.
3.2. The server
As said, our server will read and write data into a text file. Let’s see it:
simple_soap_server_class.php
", FILE_APPEND); if ($writtenBytes) { $response = "$writtenBytes bytes have been inserted."; } else { $response = "Error inserted data."; } return $response; } /** * Reads data. Invoked remotely from SOAP client. * * @return Data of file. */ public function readData() { $contents = file_get_contents(self::FILENAME); return $contents; } }
So simple, about reading and writing into a text file.
Note the functions this server implements, in lines 16 and 33. This functions will be those invoked by the client.
3.3. The client
As we said above, depending of the mode (WSDL or not WSDL), the client has to handle the connection in a different way but, once stablished, the procedure is the same. So, let’s code a class that works for both modes, handling the SoapClient class instantiation accordingly to the mode:
simple_soap_client_class.php
initializeClient($soapMode, $serverLocation); } /** * Instantiates the SoapClient, depending on the specified mode. * * For WSDL, it just has to be instantiated with the location of the service, which actually has to be the * .wsdl location. * * For non-WSDL, the first parameter of the constructor has to be null; and the second, an array specifying * both location and URI (which can be the same, the important parameter is the location). */ protected function initializeClient($soapMode, $serverLocation) { switch ($soapMode) { case self::MODE_WSDL: $this->client = new SoapClient($serverLocation); break; case self::MODE_NO_WSDL: $options = array("location" => $serverLocation, "uri" => $serverLocation); $this->client = new SoapClient(NULL, $options); break; default: throw new Exception("Error: invalid SOAP mode provided."); break; } } /** * Inserts data remotely into the SOAP service. * * @param $data Data to insert remotely. * @return Response from remote service. */ public function insertData($data) { $response = $this->client->insertData($data); return $response; } /** * Reads data from SOAP service. * * @return Data received from remote service. */ public function readData() { return $this->client->readData(); } }
The lines 35 and 45 instantiate the SoapClient class, depending on the $mode received. This is how it works for each mode:
- For WSDL mode, we have just to pass the server location to the constructor. For this mode, the location must be the WSDL definition file (.wsdl), not a PHP file .
- For non WSDL mode, the first parameter has to be null (because we are not accessing a WSDL definition). So, the location must be defined in a different way, providing an array with "location" and "uri" elements, with the server location as values. In this case, the location must be the PHP file handling the web service .
After the instantiation, the communication with the service is pretty simple. We have just to call the methods that we saw defined in SimpleSoapServer class, in simple_soap_client_class.php , through the SoapClient class instance. As we said before, we are calling methods that are defined in other place. What PHP SoapClient does, is to provide us those methods defined by the web service, and, when we call them, it will execute them in the server through the SOAP protocol that it has already implemented, with no needing to care about how it works. Seems magic, doesn’t it?
If you don’t believe it, let’s see a script to use this client.
3.4. Using the client
The following script allows us to use the client to communicate with the service, through GET parameters. These are the parameters available:
- "mode" , to specify the mode (WSDL or non WSDL).
- "action" , to specify the action to perform. Available values are "insert" and "read" .
- "value" , to specify the value to insert, only necessary when the action is "insert" .
handle_soap_request.php
getMessage());
}
return $soapClient;
}
// Flow starts here.
checkGETParametersOrDie(["mode", "action"]);
$mode = $_GET["mode"];
$action = $_GET["action"];
$soapClient = instantiateSoapClient($mode);
switch($action) {
case ACTION_INSERT:
checkGETParametersOrDie();
$value = $_GET;
try {
$response = $soapClient->insertData($value);
echo "Response from SOAP service: $response
";
} catch (Exception $exception) {
die("Error inserting into SOAP service: " . $exception->getMessage());
}
break;
case ACTION_READ:
try {
$data = $soapClient->readData();
echo "Received data from SOAP service:
";
echo $data;
} catch (Exception $exception) {
die("Error reading from SOAP service: " . $exception->getMessage());
}
break;
default:
die("Invalid "action" specified.");
break;
}
If we enter in the browser http://127.0.0.1/php_soapclient_example/handle_soap_request.php?mode=no_wsdl&action=insert&value=testing_no_wsdl , the service will create a no_wsdl/data.txt file (if not exists already), writing the provided value "testing_no_wsdl" , and the following will be printed:
Response from SOAP service: 19 bytes have been inserted.
(The 4 bytes extra correspond to additional
characters inserted into the file).
The service offers a method to reading all the data, so, if we enter in the browser http://127.0.0.1/php_soapclient_example/handle_soap_request.php?mode=no_wsdl&action=read , the following will be printed:
Received data from SOAP service:
testing_no_wsdl
We can check it also for WSDL mode, setting mode=wsdl in the parameters.
4. Considerations
As said in section 3, when we are developing a web service client (no matter if SOAP, REST, etc.), the service provider has to provide also documentation about the available methods; we need to know the definition of these: which parameters is expecting, af if it returns something. Because of that, the most usual way to develop a SOAP web service is using WSDL files, to provide documentation of the available methods. In any case, the SoapClient has a method to get the available methods, for debugging purposes, named __getFunctions() . So, we could see the offered methods by the service with the following:
Var_dump($soapClient->__getFunctions())
Assuming that $soapClient has been instanced properly.
Also note that, if the service does not offer an encrypted connection, the communication between the client and the server will be made in plain text. So, if we have to develop a client that has to deal with sensible information, we should ensure that the communication with the server can be considered safe.
5. Summary
We have seen how to develop a client for a SOAP web service using PHP’s SoapClient class. The server may offer the service using WSDL, or not, something to take into account when we are instantiating the SoapClient class. Then, to use the methods provided by the service, we just have to call those methods in SoapClient instance, as they were ours.
Для создания SOAP web-службы на PHP прежде всего необходимо провести установку и настройку PHP, а также локального HTTP-сервера. В данном проекте был использован пакет XAMP, позволяющий провести быструю установку HTTP-сервера Apache и PHP.
Отдельно необходимо внести правки в конфигурационные файлы Apache и PHP.
В конфигурационном файле HTTP-сервера Apache httpd.conf необходимо найти приведенный ниже фрагмент кода:
Для запуска PHP-скриптов на локальном сервере необходимо хранить их в корневой директории сервера Apache, по умолчанию, папка htdocs в корневой папке Apache.
Для изменения корневой директории сайта необходимо внести изменения в файл httpd.conf в строку, приведенную ниже:
Для подключения поддержки PHP в файл httpd.conf необходимо внести следующие строки:
Для запуска HTTP-сервера Apache можно использовать консольные команды, либо контрольную панель XAMMP, позволяющую управлять состоянием Apache, а также проводить его конфигурацию.
Вид данной утилиты приведен на рисунке 2.
Рисунок 2. - Утилита XAMMP Control Panel:
Для создания SOAP web-службы необходимо написать PHP документы, реализующие клиентскую и серверную стороны приложения, а также WSDL-страницу, для описания web-сервиса.
Создадим web-службу, которая предоставляет одну web-услугу, возвращающую клиенту результат выполнения арифметической операции над двумя числами. Входными элементами являются два числа, задаваемые клиентом, а также символьная переменная, определяющая арифметическую операцию.
Клиентская сторона реализована двумя PHP-документами. В файле form.php при помощи элемента