This paper describes examples in PHP soap usage.Share to you for your reference, as follows:
First, we must set up the server environment
Modify php.ini
You have to add extension = php_soap.DLL (built-load package soap)
Modify soap.wsdl_cache_enabled = 1 to soap.wsdl_cache_enabled = 0 This is the cache soap, preferably to 0 when the test on the line to a stable
There are two modes one is soap wsdl, one is no-wsdl
Second, be familiar with several functions
1. SoapServer
Defined function can be called when the server side php SoapServer for creating page data and returns the response
format:
$ Soap = new SoapServer ($ wsdl, $ array);
Among them, as was the use of soap wsdl file, wsdl is to describe the Web Service in a standard format, if the $ wsdl is set to null, it said they did not use wsdl mode.
SoapServer the attribute information, it is an array.
addFunction method SoapServer object is used to declare a function which can be called a client, syntax is as follows:
$ Soap-> addFunction ($ function_name);
Which is a SoapServer object is a function name to be called.
SoapServer object handle method for processing the user input and calls the corresponding function, and returns the result to the client processing.Syntax is as follows:
$ Soap-> handle ([$ soap_request]);
Wherein, SOAP is a $ SoapServer objects, $ soap_request is an optional parameter that indicates the user''s request for information.If no $ soap_request, it indicates that all the received user request server.
2. SoapClient
SoapClient used to call SoapServer page on a remote server, and implement a call to the appropriate function
format:
$ Soap = new SoapClient ($ wsdl, $ array);
$ Wsdl parameter and $ array with the same SoapServer
SoapClient method to create the following syntax:
$ Soap-> user_function ($ params);
Which, $ soap is a SoapClient objects, user_function is a server-side function to be called, $ params parameter passed to the function.
3. SoapFault
SoapFault soap used to generate an error that may occur during the visit.Create a soapFault object syntax
format:
$ Fault = new SoapFault ($ faultcode, $ faultstring);
Wherein the user-defined error code, the error message is a user-defined.SoapFault generated when an object is automatically generated when the server-side page error, or an object created by users themselves SoapFault.For errors that occur when Soap visit, the client may be obtained by capturing the appropriate error message SoapFalut objects. After the client SoapFault capture objects can be obtained by the error code and error message code below.
$ Fault-> faultcode; // error code
$ Fault-> faultstring; // error message
Which, $ fault is SoapFault objects created earlier.
4. SoapHeader
soapheader header for description of soap, are generally used for authentication
format:
$ H = new SoapHeader ( ''http: // 192.168.0.153 / hao / '','' auth '','' 123456789 '', false, SOAP_ACTOR_NEXT);
Third, examples
Wsdl mode without the code
Server:
// authentication server
class Test {
public function auth ($ a)
{
if ($ a != ''123456789'') {
throw new SoapFault ( ''Server'', ''You are not allowed to access'');
}
}
function say ()
{
return ''Hi11111'';
}
}
$ Srv = new SoapServer (null, array ( ''uri'' => ''http: // 192.168.0.153 / hao ''));
$ Srv-> setClass ( ''Test'');
$ Srv-> handle ();
Test classes are auth authentication judgment method corresponding to the method of the client soapheader
Client:
// encrypted client
$ Cli = new SoapClient (null, array ( ''uri'' => ''http: // 192.168.0.153 / hao / '','' location ''=>'' http: // 192.168.0.153 / hao / test.php '','' trace ''=> true,'' encoding ''=>'' utf-8 ''));
$ H = new SoapHeader ( ''http: // 192.168.0.153 / hao / '','' auth '','' 123456789 '', false, SOAP_ACTOR_NEXT);
$ Cli -> __ setSoapHeaders (array ($ h));
try {
echo $ cli-> say ();
} Catch (Exception $ e) {
echo $ e-> getMessage ();
}
soapheader auth in a corresponding method server auth
Without certification, you can remove the following two lines:
$ H = new SoapHeader ( ''http: // 192.168.0.153 / hao / '','' auth '','' 123456789 '', false, SOAP_ACTOR_NEXT);
$ Cli -> __ setSoapHeaders (array ($ h));
wsdl mode
Mr. wsdl file into the first, as to how to generate a lot of sites
Server:
// wsdl server
Require ''https: // www.jb51.net / article / server.class.php '';
$ Server = new SoapServer ( ''https: // www.jb51.net / article / server.wsdl '');
$ Server-> setClass ( ''Server'');
$ Server-> handle ();
server.class.php class code
class Server {
public function auth ($ a) {
if ($ a != ''123456789'') {
throw new SoapFault ( ''Server'', ''You are not allowed to access'');
}
}
public function test () {
return ''you are testing'';
}
}
Client:
// wsdl file client
$ Soap = new SoapClient ( ''http: // 192.168.0.153 / hao / server.wsdl ''); // If it is remote, then of course write dizzylion.The URL of the wsdl.
$ H = new SoapHeader ( ''http: // 192.168.0.153 / hao / '','' auth '','' 123456789 '', false, SOAP_ACTOR_NEXT);
$ Soap -> __ setSoapHeaders (array ($ h));
try {
echo $ soap-> test ();
} Catch (Exception $ e) {
echo $ e-> getMessage ();
}
More about PHP-related content and interested readers can view the site topic: "php socket usage summary", "php string (string) Usage summary", "PHP math skills summary", "php Object-Oriented Programming Guide "," PHP array (array) operating skills Daquan "," PHP data structures and algorithms tutorial "," php programming algorithm summary "and" PHP web programming skills summary "
In this paper, we hope that the PHP programming help.
You may also be interested in the article: PHP programmer to carry out simple service governance structure Operational Details (B) PHP programmer to carry out simple service governance structure Operational Details (a) php curl and soap in the way for a solution of the problem of using PHP service timeout NuSOAP method calls Web services in PHP soap usage examples using PHP SOAP extension methods to achieve WebService PHP using SOAP calls.WebService method of data net of PHP Class SoapClient not found solutions implemented in PHP PHP Soap communicate using SOAP API calls PHP programmer simple example of the operation of the service to carry out governance structure Operational Details (c)