Communication between the soap and nusoap servers and clients of webServicephp

Source: Internet
Author: User
: This article mainly introduces the communication between the webServicephp soap server and the nusoap server and the client. For more information about PHP tutorials, see. Php soap server

It is very easy to create a Soap server using PHP and SOAP. Basically, you only need to write the functions that you want to expose to your Web service, and then use Soap to register them. In addition, two steps are required to establish the php soap server. First, you need to create an instance of the Soap object in your PHP code, and then use the http post method to transmit the original data to Soap for processing.

When php5 is integrated with soap, SOAP is easy to use. The most common classes are SoapServer and SoapClient. SoapServer is used to create a Webservice server and SoapClient is used to call Webservice, that 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:

1

extension=php_soap.dll

Soap instance:

1. create a server first. because the integrated php5 does not need to introduce any soap support files, directly create the server. for example, set the access address to http: // 127.0.0.1/test. php.

01

02

03

04

05

06

07

08

09

10

11

classtest{

functiondemo(){

return'hello word';

}

}

// Start creating webservice

// Null indicates the ip address. The second parameter is similar to the authorization ID.

$webService= newSoapServer(null,array('uri'=>'test.php'));

// Set the class to be provided. isn't setClass easy to understand?

$webService->setClass('test');

$webService->handle();

So far, the soap server has been created.

2. call the newly created soap service. Similarly, since the integrated php5 does not need to introduce any soap support file, you can directly call it.

1

2

3

4

5

6

7

$client= newSoapClient(null,array(

"location"=> 'http://127.0.0.1/test.php',

"uri" => 'test.php', // Request ID, which must correspond to the server and client

));

// At this point, you can call the method in the class.

$demo= $client->demo();

var_dump($demo);

However, for Nusoap, the flexibility is stronger than soap, and their operation processes are basically the same, but there is a slight deviation in the processing process. NuSOAP is also relatively simple to use, the most common classes are soap_server and nusoap_client. soap_server is used to create the Webservice server, and nusoap_client is used to call Webservice, that is, the client. These two classes are defined in lib/nusoap. php. Therefore, we need to reference this file when creating or calling the Webservice interface program.

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

Nusoap Download: nusoap-0.9.5

Nusoap instance:

1. The first thing we need to do is to create a server. we just mentioned a little deviation. we just created this on the server. First, we introduced the nusoap support class library.

01

02

03

04

05

06

07

08

09

10

11

// Introduce the nusoap support class library

require_once('lib/nusoap.php');

$soap= newsoap_server();

$soap->configureWSDL('test');

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

$soap->register('GetTestStr',

array("name"=> "xsd:string"),// Parameter. the default value is "xsd: string"

array("return"=> "xsd:string")

);

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

$soap->service($HTTP_RAW_POST_DATA);

Now, the nusoap server has been created, assuming the address is http: // 127.0.0.1/test. php.

2. call nusoap. Before that, I encapsulated the File class. nuSoapApi. php. the code is as follows.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

/**

* Nusoap extension class

* Email:zhangyuan#tieyou.com

* @author hkshadow

*/

// Introduce the 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 (response) of the incoming message ). Default UTF-8

private$_arrParam= array();//arrData

private$_objSoap= null; // Initialize the Nusoap object

/**

* Constructor

* @param str $_strSoapUrl

* @ Param (optional) the default value is wsdl $ _ strSoapWsdl.

*/

publicfunction__construct($_strSoapUrl,$_strSoapWsdl= true){

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

$this->_objSoap =newnusoap_client($_strSoapUrl,$_strSoapWsdl);

}

}

/**

* Set message data

* @param array $arrData

*/

publicfunctionsetArrParam($arrData){

$this->_arrParam =$arrData;

}

/**

* Set xml encoding

* @param true / false $bool

*/

publicfunctionsetDeCodeUtf8($bool= false){

$this->_objSoap->decode_utf8 =$bool;

}

/**

* Sets the character encoding of the http content type.

* @param str $strCode

*/

publicfunctionsetSoapDefenCoding($strCode){

if(!empty($strCode)){

$this->_objSoap->soap_defencoding =$strCode;

}else{

$this->_objSoap->soap_defencoding =$this->_strSoapDefenCoding;

}

}

publicfunctionsetXmlEnCoding($strCode){

if(!empty($strCode)){

$this->_objSoap->xml_encoding =$strCode;

}else{

$this->_objSoap->xml_encoding =$this->_strXmlEnCoding;

}

}

/**

* Getting data

*/

publicfunctiongetRequestData($fun){

$arrData= array();

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

return$arrData;

}

/**

* Array to object

* @param array $arrData

*/

publicfunctionarrDataObj($arrData){

// Reference address instead of reference copy

$objStdClass= newstdClass();

foreach($arrDataas$key=>$value){

if(is_array($value)){

$objStdClass->$key= $this->arrDataObj($value);

}else{

$objStdClass->$key= $value;

}

}

return$objStdClass;

}

}

?>

Call the execution process from here:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

// Define the webservice request url

define('SERVICEDTS_WEBSERVER_URL','http://127.0.0.1/test.php?wsdl');

// Introduce the encapsulated class

require_once('lib/class.nuSoapApi.php');

// Call the 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');

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

echo"Returned results :", $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 read format that provides all the information required to access the Service to the Web Service client. NuSOAP provides a class to parse WDSL files and extract information from them. Soapclient objects use the wsdl class to make it easier for developers to call services. With the help of WSDL information, the programmer can call a message by knowing the operation name and parameters.

Using WSDL through NuSOAP provides the following advantages:
All service meta files, such as namespaces, endpoint URLs, and parameter names, can be obtained directly from the WSDL file, this allows the client to dynamically adapt to changes on the server. Because the data can be obtained from the server at any time, you do not need to use hard encoding in the user script.
It allows us to use the soap_proxy class. This class is derived from the soapclient class and adds the methods corresponding to the operations listed in the WDSL file. Now you 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. It adds methods corresponding to the operations defined in the WSDL document and allows users to call an endpoint remote method. This is only applicable when the soapclient object is initialized using the WDSL file. The advantage is that it is easy for users, but the disadvantage is that it is time-consuming to create objects in PHP and does not serve utilitarian purposes (and this functionality serves no utilitarian purpose ).

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

// Call the 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 a 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('><',">\n<", $p->response).'</xmp>';

Run the created Server url, http: // 127.0.0.1/test. php. the execution result is as follows.

Click the method name. By adding several lines of code to the service, we can use NuSOAP to provide a visual document for the service. However, this is not all we can do.

We add some WSDL calls to the service by using NuSOAP. we can generate the WSDL for the service and some other documents. The difference is that we can do less 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 soapclent class is parsed by providing the wsdl url, instead of using the service endpoint.

When NuSoap calls WebService, you can set the encoding. the solution to garbled code is as follows:

1

2

3

4

$client= newnusoap_client("http://127.0.0.1/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 during the call:
XML error parsing SOAP payload on line x (row number): Reserved XML Name

If the SoapClient class of nusoap conflicts with the built-in soap class of php5 when php5 is enabled (I do not have such a situation here, two types are available at the same time ):
Solution
1. modify php. ini and do not load the built-in soap extension of php5 (php_soap.dll in windows ).
2. you have also changed the name of the nusoap SoapClient class.

At this point, both the built-in soap of php5 and the nusoap extension class are good, while webservice is a solution. from the preceding examples, nusoap is more flexible, for simple webserice communication, the built-in soap in php5 is faster, either of which can be used.

Address: http://www.mudbest.com/webservicephp%E7%9A%84soap%E4%B8%8Enusoap%E6%9C%8D%E5%8A%A1%E7% AB %AF%E4%B8%8E%E5% AE %A2%E6%88%B7%E7% AB %AF%E7%9A%84%E9%80%9A%E4%BF%A1/

The above introduces the communication between the webServicephp soap server and the nusoap server and the client, including some content, and hope to help friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.