SoFunction
Updated on 2025-03-03

Example of soap usage in PHP [SoapServer Server and SoapClient Client Writing]

This article describes the usage of soap in PHP. Share it for your reference, as follows:

1. First, set up the server environment

Revise

Need to addextension=php_soap.dll(Loading the soap built-in package)

Revisesoap.wsdl_cache_enabled=1Change tosoap.wsdl_cache_enabled=0This is the cache of soap. It is best to change it to 0 during testing, and it is changed to 1 if it is stable when it is launched.

There are two modes of soap: wsdl and no-wsdl

2. Familiar with several functions

1. SoapServer

SoapServer is used to define functions that can be called and return response data when creating php server-side pages.

Format:

$soap = new SoapServer($wsdl,$array); 

in,$wsdlFor soap, use wsdl file. wsdl is a standard format that describes Web Service. If $wsdl is set to null, it means that the wsdl mode is not used.
$arrayIt is the property information of SoapServer, which is an array.
The addFunction method of the SoapServer object is used to declare which function can be called by the client. The syntax format is as follows:

$soap->addFunction($function_name);

in,$soapis a SoapServer object.$function_nameis the name of the function that needs to be called.

The handle method of the SoapServer object is used to process user input and call the corresponding function, and finally returns the result processed by the client. The syntax format is as follows:

$soap->handle([$soap_request]); 

Among them, $soap is a SoapServer object, and $soap_request is an optional parameter to represent the user's request information. If $soap_request is not specified, it means that the server will receive all requests from the user.

2. SoapClient

SoapClient is used to call the SoapServer page on the remote server and implements the call to the corresponding function.

Format:

$soap = new SoapClient($wsdl,$array); 

Parameters $wsdl and $array are the same as SoapServer

The SoapClient method, the creation syntax is as follows:

$soap->user_function($params); 

Among them, $soap is a SoapClient object, user_function is the function to be called on the server side, and $params is the parameter to be passed into the function.

3. SoapFault

SoapFault is used to generate errors that may occur during soap access. Create a syntax format for a soapFault object

Format:

$fault = new SoapFault($faultcode,$faultstring); 

in,$faultcodeis a user-defined error code.$faultstringIt is a user-defined error message. The soapFault object will be automatically generated when an error occurs on the server-side page, or when the user creates the SoapFault object by himself. For errors that occur during Soap access, the client can obtain the corresponding error information by capturing the SoapFalut object. After the client captures the SoapFault object, the error code and error information can be obtained through the following code.

$fault->faultcode;//Error code$fault->faultstring;//error message

Where $fault is the SoapFault object created in the previous section.

4. SoapHeader

soapheader is used to describe the header information of soap, and is generally used for authentication.

/*
  *SoapHeader parameters are as follows:
  *'/' namespace (namespace can be omitted)
  *'MySoapHeader' Class name of SoapHeader header
  *'array(...)' Store string parameters that identifies the identity
  *'true' Does the header have to be processed
 */

Format:

$h = new SoapHeader('http://192.168.0.153/hao/', 'auth', '123456789', false, SOAP_ACTOR_NEXT);

III. Example

Code without wsdl mode

Server:

//Certification serviceclass Test{
  public function auth($a)
  {
    if($a != '123456789'){
      throw new SoapFault('Server', 'You do not have access');
    }
  }
  function say()
  {
    return 'Hi11111';
  }
}
$srv = new SoapServer(null, array('uri' => 'http://192.168.0.153/hao'));
$srv->setClass('Test');
$srv->handle();

In the Test class, auth is authentication judgment. This method corresponds to the method in 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/', '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();
}

Auth corresponding server auth method in soapheader

If you do not authenticate, 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

First, I created a wsdl file, and as for how to generate a website, I have many

Server:

//wsdl serverRequire './';
$server = new SoapServer('./');
$server->setClass('Server');
$server->handle();

Class Code

class Server {
  public function auth($a){
    if($a != '123456789'){
    throw new SoapFault('Server', 'You do not have access');
    }
  }
  public function test() {
    return 'you are testing';
  }
}

Client:

//wsdl file client$soap = new SoapClient('http://192.168.0.153/hao/'); //If it is remote, of course it will be written as a URL.$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();
}

For more information about PHP related content, please check out the topic of this site:Summary of usage of php socket》、《Summary of usage of php strings》、《Summary of PHP mathematical operation skills》、《PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm"and"Summary of PHP network programming skills

I hope this article will be helpful to everyone's PHP programming.