Home  >  Article  >  Backend Development  >  Communication between webServicephp's soap and nusoap server and client

Communication between webServicephp's soap and nusoap server and client

WBOY
WBOYOriginal
2016-07-30 13:29:26969browse

PHP SOAP Server

It is very easy to set up a SOAP server with PHP and Soap. Basically, you just write the functions you want to expose to your Web service, and then register them with Soap. There are also two steps required to complete the establishment of the PHP SOAP server. First, you have to create an instance of the Soap object in your PHP code, and then use the HTTP POST method to pass the original data to Soap for processing

Soap has been integrated in php5. The use of SOAP is relatively simple, and the most commonly used one is The classes are SoapServer and SoapClient, where SoapServer is used to create the Webservice server, and the SoapClient class is used to call the Webservice, which is the client. Since soap is integrated from php5, you only need to enable the soap component in php.ini.

Take Windows as an example:

Communication between webServicephps soap and nusoap server and client

extension=php_soap.dll

Soap instance:

Communication between webServicephps soap and nusoap server and client. First create a service end, since the integrated php5 does not need to introduce any soap support files, it can be created directly. For example, the access address is set to http://Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client7.0.0.Communication between webServicephps soap and nusoap server and client/test.php.

3

0Communication between webServicephps soap and nusoap server and client

0Communication between webServicephps soap and nusoap server and client

03

04

05

06

07

08

09

Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

classtest{

                                                                                       }}

//Start creating webservice //null can be passed in ip, the second parameter is similar to the authorization ID, which will be used when calling

$webService= new

SoapServer(null,array

( 'uri'

=>'test.php'

));//Set the class that needs to be provided. SetClass is not difficult to understand, right? $webService->setClass('test');$webService-> handle();

That’s it, soap The server is created. Communication between webServicephps soap and nusoap server and client. Call the soap service you just created. Similarly, since the integrated php5 does not need to introduce any soap support files, you can just call it directly.

Communication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and client
45

6

7

$client

=

new

SoapClient(null,

array

(

)
"location"

=> 'http://Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client7.0.0.Communication between webServicephps soap and nusoap server and client/test.php', "uri" => 'test.php'

, //Request identification, the server and the client must correspond));//At this point, you can call the method in the class

$demo= $client ->demo();var_dump(

$demo);

However, Nusoap is more flexible than soap. Their operation processes are basically the same, but there are slight deviations in the processing process. The use of NuSOAP is also relatively simple. The most commonly used classes are soap_server and nusoap_client, among which soap_server It is used to create the Webservice server, and the class nusoap_client is used to call the Webservice, which is the client. The definitions of these two classes are in lib/nusoap.php, so we need to reference this file when creating or calling the Webservice interface program.

NuSoap is a WebService programming tool in PHP environment, used to create or call WebService. It is an open source software, which is a series of PHP classes written entirely in PHP language that sends and receives SOAP messages through HTTP. It is developed by NuSphere Corporation (http://dietrich.ganx4.com/nusoap/). One advantage of NuSOAP is that it does not require extension library support. This feature allows NuSoap to be used in all PHP environments and is not affected by server security settings.

Provide nusoap download: nusoap-0.9.5

Nusoap instance:

Communication between webServicephps soap and nusoap server and client. The first thing to do is still to create the server. I just mentioned that there is a slight deviation, so I created this on the server. First introduce nusoap Support class library

0Communication between webServicephps soap and nusoap server and client

0Communication between webServicephps soap and nusoap server and client

03

04

05

06

07

08

09

Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

/ /Introduce nusoap support class library

require_once('lib/nusoap.php');

$soap= newsoap_server();

$ soap->configureWSDL('test');

//Here we need to explain the register function. The first parameter is the method that needs to be called, and the second parameter is the incoming data , the third parameter is the outgoing data.

$soap->register('GetTestStr',

) > "xsd:string" ), // Parameter, the default is "xsd:string"                                                                                                                                                                               ) ;

$HTTP_RAW_POST_DATA= isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA: ''

;$soap

->service($HTTP_RAW_POST_DATA);

At this point, the nusoap server has been created, assuming the address is http://Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client7.0.0.Communication between webServicephps soap and nusoap server and client/test.php.

Communication between webServicephps soap and nusoap server and client. Call nusoap. Before that, I encapsulated it here. The file is class.nuSoapApi.php and the code is as follows.

Communication between webServicephps soap and nusoap server and client6Communication between webServicephps soap and nusoap server and client7

0Communication between webServicephps soap and nusoap server and client

0Communication between webServicephps soap and nusoap server and client

03

04

05

06

07

08

09

Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and client3

Communication between webServicephps soap and nusoap server and client4

Communication between webServicephps soap and nusoap server and client5

Communication between webServicephps soap and nusoap server and client6

Communication between webServicephps soap and nusoap server and client7

Communication between webServicephps soap and nusoap server and client8

Communication between webServicephps soap and nusoap server and client9

Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and client3

Communication between webServicephps soap and nusoap server and client4

Communication between webServicephps soap and nusoap server and client5

Communication between webServicephps soap and nusoap server and client6

Communication between webServicephps soap and nusoap server and client 7

Communication between webServicephps soap and nusoap server and client8

Communication between webServicephps soap and nusoap server and client9

30

3Communication between webServicephps soap and nusoap server and client

3Communication between webServicephps soap and nusoap server and client

33

34

35

36

37

38

39

40

4Communication between webServicephps soap and nusoap server and client

4Communication between webServicephps soap and nusoap server and client

43

44

45

46

47

48

49

50

5Communication between webServicephps soap and nusoap server and client

5Communication between webServicephps soap and nusoap server and client

53

54

55

56

57

58

59

60

6Communication between webServicephps soap and nusoap server and client

6Communication between webServicephps soap and nusoap server and client

63

64

65

66

67

68

69

70

7Communication between webServicephps soap and nusoap server and client

7Communication between webServicephps soap and nusoap server and client

73

74

75

76

7 7

78

79

80

8Communication between webServicephps soap and nusoap server and client

8Communication between webServicephps soap and nusoap server and client

83

84

85

86

87

88

89

90

<?php

/**

* nusoap extension class

* Email:zhangyuan#tieyou.com

* @author hkshadow

*/

//Introduce nusoap base class

require_once('pto/nusoap/nusoap_new/lib/nusoap.php');

classnuSoapApi extendsnusoap_client{

private$_strSoapUrl= '';//wsdl url

private $_strSoapDefenCoding= 'utf-8' ; //Character set of the HTTP content type of the current request. Default utf-8

​​​

private$_strXmlEnCoding= 'utf-8';//Character set encoding of the incoming message (response). Default utf-8

​​​

private$_arrParam= array(); //arrData

private$_objSoap= null; //Initialize Nusoap object

     

/**

* Constructor

* @param str $_strSoapUrl

sdl

​​​

*/

public

function__construct($_strSoapUrl,$_strSoapWsdl= true){ (

$this->_objSoap === null){                                                                                                  newnusoap_client(

$_strSoapUrl,$_strSoapWsdl); }

/**

* Set message data

* @param array $arrData

*/

​​​​​​​​​​​

function

setArrParam($arrData

){

​​​​​​​​​​

$this

-&g t;_arrParam = $arrData; }

/**

* Set xml encoding

* @param true / false $bool

*/

public

functionsetDeCodeUtf8(

$bool

= false){<p><code>        $this->_objSoap->decode_utf8 = $bool;

    }

     

    /**

* Set the character encoding of the http content type

* @param str $strCode

*/

    publicfunctionsetSoapDefenCoding($strCode){

<p><code>        if(!empty($strCode)){

<p><code>            $this->_objSoap->soap_defencoding = $strCode;

<p><code>        }else{

<p><code>            $this->_objSoap->soap_defencoding = $this->_strSoapDefenCoding;

<p><code>        }

    }

    publicfunctionsetXmlEnCoding($strCode){

<p><code>        if(!empty($strCode)){

<p><code>            $this->_objSoap->xml_encoding = $strCode;

<p><code>        }else{

<p><code>            $this->_objSoap->xml_encoding = $this->_strXmlEnCoding;

<p><code>        }

    }

     

    /**

* Get data

*/

    publicfunctiongetRequestData($fun){

<p><code>        $arrData= array();

<p><code>        $arrData= $this->_objSoap->call($fun,$this->_arrParam);

<p><code>        return$arrData;

    }

    /**

<p><code>                                                                                                                                                                                                                                       Array to        */

    

publicfunction

arrDataObj(

$arrData){

<p><code>        //引用地址,而非引用拷贝        $objStdClass=

new

stdClass();

        

foreach($arrDataas$key

=>

$value){            if(is_array($value

)){

                $objStdClass->$key= $this

->arrDataObj(

$value);<p><code>                 }else{

<p><code>                                                                                                                                           = $value;                                                      }

                                                                                     

}

?> 0Communication between webServicephps soap and nusoap server and client

03

0405 0607

08

09Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and client3

Communication between webServicephps soap and nusoap server and client4

Communication between webServicephps soap and nusoap server and client5

Communication between webServicephps soap and nusoap server and client8

Communication between webServicephps soap and nusoap server and client9

// Define the request url of the webserviceNusoapApi(SERVICEDTS_WEBSERVER_URL,true);

define(

'SERVICEDTS_WEBSERVER_URL'

,

'http://Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client7.0.0.Communication between webServicephps soap and nusoap server and client/test.php?wsdl'

);

//Introduce the just encapsulated class

require_once

(

'lib/class.nuSoapApi.php '

);

//Call nusoap extension class

$client

=

new

$ client

->setSoapDefenCoding(

'utf-8');$client->setDeCodeUtf8(false);

$client

->setXmlEnCoding(

'utf-8' ); $paras

=

array

(

'name'=> 'hkshadow');

$client ->setArrParam($ paras);

$result=

$client

->getRequestData('GetTestStr');

if

(! $err= $client->getError ()) {​​​​echo

" Return result: "

, $result;

}

else{ echo" Call error: "

,

$err;}//Output Hello, { hkshadow } !

Explanation:

WSDL
WSDL is an XML language used to describe Web Services. It is a machine-readable format that provides all the information necessary to access the service to the Web Service client. NuSOAP specifically provides a class to parse WDSL files and extract information from them. The soapclient object uses the wsdl class to make it easier for developers to call services. By creating the message with the help of WSDL information, the programmer only needs to know the name and parameters of the operation to call it.

Using WSDL through NuSOAP provides the following advantages:
All service metafiles, such as namespaces, endpoint URLs, parameter names, etc., can be obtained directly from the WSDL file, thus allowing clients Dynamically adapt to server-side changes. Because this data is always available from the server, this data no longer needs to be hard-coded in user scripts.
It allows us to use soap_proxy class. This class is derived from the soapclient class and adds methods corresponding to the operations detailed in the WDSL file. Now users can directly call these methods through it.
The soapclient class contains a getProxy() method, which returns an object of the soap_proxy class. The soap_proxy class is derived from the soapclient class, adds methods corresponding to the operations defined in the WSDL document, and allows the user to call an endpoint's remote method. This only applies if the soapclient object is initialized with a WDSL file. The advantage is ease of use for users, the disadvantage is performance – creating objects in PHP is time-consuming – and this functionality serves no utilitarian purpose.

0Communication between webServicephps soap and nusoap server and client

0Communication between webServicephps soap and nusoap server and client

03

04

05

06

07

08

09

Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and client3

Communication between webServicephps soap and nusoap server and client4

Communication between webServicephps soap and nusoap server and client5

Communication between webServicephps soap and nusoap server and client6

Communication between webServicephps soap and nusoap server and client7

Communication between webServicephps soap and nusoap server and client8

Communication between webServicephps soap and nusoap server and client9

Communication between webServicephps soap and nusoap server and client0

Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client

//Call nusoap extension class

$client= newNusoapApi(SERVICEDTS_WEBSERVER_URL,true );

$client->setSoapDefenCoding('utf-8');

$client->setDeCodeUtf8(false);

$client ->setXmlEnCoding('utf-8');

$paras= array('name'=> 'hkshadow' );

$client->setArrParam($paras);

// $result = $client->getRequestData('GetTestStr');

//Generate proxy Class

$proxy= $client->getProxy();

//Call remote function

$sq= $proxy -> GetTestStr('Bruce Lee');

if(!$err=$proxy->getError()) {

print_r( $sq);

} else{

print"ERROR: $err";

}

print' REQUEST:<xmp>'.$p->request.'</xmp>';

print'RESPONSE:<xmp>' . str_replace('><code>, ">n<code>, $p->response).'</xmp>';

Run the server url you just created, http://Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client7.0.0.Communication between webServicephps soap and nusoap server and client/test.php. The results after execution are as follows.
Communication between webServicephps soap and nusoap server and client

Click on the method name. In this way, by adding a few lines of code to the service, we provide a visual document for the service using NuSOAP. But that's not all we can do.

Communication between webServicephps soap and nusoap server and client

We add some WSDL calls in the service by using NuSOAP. We can generate WSDL and some other documents for the service. In contrast, there is not much we can do in the client, at least in our simple example. The client shown below is no different from the client that does not use WSDL. The only difference is that parsing the soapclent class is done by providing the URL of the WSDL instead of the service endpoint as before.

NuSoap can set the encoding when calling WebService. The solution to garbled codes is as follows:

Communication between webServicephps soap and nusoap server and client

Communication between webServicephps soap and nusoap server and client

3

4

$client = newnusoap_client("http://Communication between webServicephps soap and nusoap server and clientCommunication between webServicephps soap and nusoap server and client7.0.0.Communication between webServicephps soap and nusoap server and client/test.php?wsdl",true);

$client->soap_defencoding = 'utf-8';

$client->decode_utf8 = false;

$client->xml_encoding = 'utf-8';

The file code cannot have any output, otherwise an error similar to the following will be reported when calling:
XML error parsing SOAP payload on line x (line number): Reserved XML Name

If there is a conflict between nusoap's SoapClient class and php5's built-in SOAP class when turning on php5's built-in soap (I don't have such a situation here, both are open at the same time):
Solution
Communication between webServicephps soap and nusoap server and client. Modify php. ini does not load the built-in soap extension of php5 (php_soap.dll under windows).
Communication between webServicephps soap and nusoap server and client. Some people also renamed the SoapClient class of nusoap.

At this point, whether it is php5’s built-in soap or nusoap extension class, webservice is a solution. From some of the examples above, nusoap is more flexible, and for simple webserice communication, php5 The built-in soap is faster, no matter which one you choose, just choose one.

Original address: http://www.mudbest.com/webservicephp%E7%9A%84soap%E4%B8%8Enusoap%E6%9C%8D%E5%8A%ACommunication between webServicephps soap and nusoap server and client%E7%AB%AF%E4%B8 %8E%E5%AE%ACommunication between webServicephps soap and nusoap server and client%E6%88%B7%E7%AB%AF%E7%9A%84%E9%80%9A%E4%BF%ACommunication between webServicephps soap and nusoap server and client/

The above introduces the communication between the soap and nusoap server and client of webServicephp, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn