PHP Tutorials - Herong's Tutorial Examples - Version 4.20, by Dr. Herong Yang
HelloServer.php - First SOAP Server Application
This section provides a tutorial example on how to write a simple SOAP server application. The HelloServer.php example defines a hello() function, adds it to the SOAP server, waits for the client program to call.
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-2019, 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 Server. It has already configured correctly to interface with PHP CGI. All I have to do is to move my server application to Apache document directory:
C:\herong> copy HelloServer.php \apache\htdocs
To test my server application, I wrote this client application, HelloClient.php:
<?php # HelloClient.php #- Copyright (c) 2007-2019, 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()); ?>
Check your Web server to make sure it is running. Then run HelloClient.php. You will get:
Returning value of __soapCall() call: Hello world! Dumping request headers: POST /HelloServer.php HTTP/1.1 Host: localhost Connection: Keep-Alive 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 Server: Apache Connection: close Content-Type: text/xml; charset=utf-8 Content-Length: 522 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:
IIS 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 |
Last update: 2019.
Table of Contents
Introduction and Installation of PHP 7.3
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
►SOAP Server Functions and Examples
SoapServer - SOAP Server Class and Functions
►HelloServer.php - First SOAP Server Application
HelloServer12.php - First SOAP 1.2 Server Application
HelloServerWsdl.php - SOAP 1.2 Server Application in WSDL Mode
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers