SOAP Web Service Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
HelloServer.php - First SOAP Server Application
This section describes a tutorial example of a complete SOAP application with both server and client programs.
Okay. Now let's build our first SOAP server. I want the first server to perform a very simple function of return a greeting string based on the input name. Here is my version called, HelloServer.php:
<?php
# HelloServer.php
# Copyright (c) 2007 HerongYang.com. All Rights Reserved.
#
function hello($someone) {
return "Hello " . $someone . "!";
}
$server = new SoapServer(null,
array('uri' => "urn://www.herong.home/res"));
$server->addFunction("hello");
$server->handle();
?>
The sever application is ready. Note that:
The HTTP server I will be using is the Apache Web server. It has already configured correctly to interface with PHP CGI. For details, see my other book: "Herong's Tutorial Notes on PHP". All I have to do is to move my server application to the Web server document directory:
herong> copy HelloServer.php \local\apache\htdocs
To test my server application, I wrote this client application, HelloClient.php:
<?php
# HelloClient.php
# Copyright (c) 2007 HerongYang.com. All Rights Reserved.
#
$client = new SoapClient(null, array(
'location' => "http://localhost/HelloServer.php",
'uri' => "urn://www.herong.home/req",
'trace' => 1 ));
$return = $client->__soapCall("hello",array("world"));
echo("\nReturning value of __soapCall() call: ".$return);
echo("\nDumping request headers:\n"
.$client->__getLastRequestHeaders());
echo("\nDumping request:\n".$client->__getLastRequest());
echo("\nDumping response headers:\n"
.$client->__getLastResponseHeaders());
echo("\nDumping response:\n".$client->__getLastResponse());
?>
Make sure my Apache Web server is running. Then run HelloClient.php:
herong> php HelloClient.php Returning value of __soapCall() call: Hello world! Dumping request headers: POST /HelloServer.php HTTP/1.1 Host: localhost Connection: Keep-Alive User-Agent: PHP-SOAP/7.0.2 Content-Type: text/xml; charset=utf-8 SOAPAction: "urn://www.herong.home/req#hello" Content-Length: 499 Dumping request: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn://www.herong.home/req" 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-ENV:Body> <ns1:hello> <param0 xsi:type="xsd:string">world</param0> </ns1:hello> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Dumping response headers: HTTP/1.1 200 OK Date: Sun, 02 Sep 2018 18:31:47 GMT Server: Apache/2.4.12 (Win32) PHP/7.0.2 X-Powered-By: PHP/7.0.2 Content-Length: 522 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/xml; charset=utf-8 Dumping response: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn://www.herong.home/res" 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-ENV:Body> <ns1:helloResponse> <return xsi:type="xsd:string">Hello world!</return> </ns1:helloResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
Very exciting, right? Both server and client work nicely.
If you are interested in how the execution was carried out on the server, I have a simplified execution flow diagram on the server side:
Apache PHP (SOAP Extension) HelloServer.php
HTTP request |
-------------->|
SOAP message |
| CGI | PHP CGI API
|------->|------------------------>|
| addFunction()
|
SOAP Extension API | handle()
|<------------------------|
|
| SOAP Extension API
|------------------------>|
SOAP Extension API | hello()
|<------------------------|
CGI |
|<-------|
HTTP response |
<--------------|
SOAP message |
Table of Contents
Introduction to SOAP (Simple Object Access Protocol)
SOAP Message Transmission and Processing
SOAP MEP (Message Exchange Patterns)
PHP SOAP Extension Client Programs
►PHP SOAP Extension Server Programs
PHP SOAP Extension Functions for Server Programs
►HelloServer.php - First SOAP Server Application
HelloServer12.php - SOAP 1.2 Server Application
HelloServerWsdl.php - SOAP 1.2 Server in WSDL Mode
HelloServerWsdl2.php - SOAP 1.1/1.2 Server in WSDL Mode
PHP SOAP Web Service Example - getTemp
Perl SOAP::Lite - SOAP Server-Client Communication Module
Perl Socket Test Program for HTTP and SOAP
Perl SOAP::Lite for NumberToWords SOAP 1.1 Web Service
Perl SOAP::Lite for SOAP 1.2 Web Services
Java Socket and HttpURLConnection for SOAP
SAAJ - SOAP with Attachments API for Java
SoapUI - SOAP Web Service Testing Tool
WS-Security - SOAP Message Security Extension
WS-Security X.509 Certificate Token
Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service
Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services
Perl SOAP::Lite 0.710 for WSDL