webservicephp soap and Nusoap server-to-client communication

Source: Internet
Author: User
Tags http post php soap server
PHP SOAP Server

It is easy to build SOAP servers with PHP and SOAP. Basically, you just write out the functions you want to expose to your Web service, and then use soap to register them. Another two steps will be required to complete the creation of the PHP soap server. First you also create an instance of the SOAP object in your PHP code, and then use the HTTP POST method to pass the raw data to soap for processing

SOAP is being integrated in php5, and the use of soap is relatively straightforward, with the most commonly used classes being soapserver and SoapClient, where soapserver is used to create the WebService server, The class soapclient is used to call WebService, which is the client. Since soap is integrated from PHP5, only the SOAP component can be opened in php.ini.

Take windows as an example:

1

extension=php_soap.dll

Soap instance:

1, first create the server side, because the integrated PHP5 does not need to introduce any SOAP support files, directly created, such as the access address set to http://127.0.0.1/test.php.

01

02

03

04

05

06

07

08

09

10

11

classtest{

functiondemo(){

return'hello word';

}

}

//开始创建webservice

//null可传入ip,第二个参数类似于授权标识,调用时会用到

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

//设置需要提供的类,setClass不难理解吧?

$webService->setClass('test');

$webService->handle();

At this point, the SOAP service-side creation is complete.

2, call the SOAP service just created, similarly, because the integrated PHP5 does not need to introduce any SOAP support files, directly call.

1

2

3

4

5

6

7

$client= newSoapClient(null,array(

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

"uri"=> 'test.php', //请求标识,服务器和客户端必须对应

));

//至此,便可调用类里面的方法了

$demo= $client->demo();

var_dump($demo);

However, for Nusoap, the flexibility is stronger than soap, their operation process is basically consistent, but the processing process has a little deviation, the use of Nusoap is relatively simple, the most commonly used classes are soap_server and nusoap_client, where SOAP_ The server is used to create the WebService service side, and class nusoap_client is used to invoke WebService, which is the client. The definitions of these two classes are all in lib/nusoap.php, so we need to reference the file when we create or invoke the WebService interface program.

Nusoap is a WebService programming tool for creating or invoking webservice in a PHP environment. It is an open source software, developed by Nusphere Corporation (http://dietrich.ganx4.com/nusoap/), a series of PHP classes written entirely in PHP that send and receive SOAP messages over HTTP. One advantage of Nusoap is that there is no need for extended library support, which allows Nusoap to be used in all PHP environments and is not affected by server security settings.

Available nusoap Download: nusoap-0.9.5

Examples of Nusoap:

1, the first thing to do is still to create a service side, just said there is a little deviation, on the server to create this, first introduced Nusoap support class library

01

02

03

04

05

06

07

08

09

10

11

//引入nusoap支持类库

require_once('lib/nusoap.php');

$soap= newsoap_server();

$soap->configureWSDL('test');

//这里要说明一下register这个函数,第一个参数是需要调用的方法,第二个参数是传入的数据,第三个参数是传出的数据。

$soap->register('GetTestStr',

array("name"=> "xsd:string"),// 参数,默认为 "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);

At this point, the NUSOAP server has been created, assuming the address is http://127.0.0.1/test.php.

2, call Nusoap. Before this I encapsulated a bit here, the file is 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

21st

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扩展类

* Email:zhangyuan#tieyou.com

* @author hkshadow

*/

//引入nusoap基类

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

classnuSoapApi extendsnusoap_client{

private$_strSoapUrl= '';//wsdl url

private$_strSoapDefenCoding= 'utf-8'; // 当前请求的HTTP内容类型的字符集。默认utf-8

private$_strXmlEnCoding= 'utf-8';//传入消息的字符集编码(响应)。默认utf-8

private$_arrParam= array();//arrData

private$_objSoap= null; //初始化Nusoap对象

/**

* 构造函数

* @param str $_strSoapUrl

* @param 可选,默认为wsdl $_strSoapWsdl

*/

publicfunction__construct($_strSoapUrl,$_strSoapWsdl= true){

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

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

}

}

/**

* 设置消息数据

* @param array $arrData

*/

publicfunctionsetArrParam($arrData){

$this->_arrParam =$arrData;

}

/**

* 设置xml编码

* @param true / false $bool

*/

publicfunctionsetDeCodeUtf8($bool= false){

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

}

/**

* 设置http内容类型的字符编码

* @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;

}

}

/**

* 获取数据

*/

publicfunctiongetRequestData($fun){

$arrData= array();

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

return$arrData;

}

/**

* 数组转对象

* @param array $arrData

*/

publicfunctionarrDataObj($arrData){

//引用地址,而非引用拷贝

$objStdClass= newstdClass();

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

if(is_array($value)){

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

}else{

$objStdClass->$key= $value;

}

}

return$objStdClass;

}

}

?>

From here, call the execution process:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

//定义webservice的请求url

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

//引入刚刚封装的类

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

//调用nusoap扩展类

$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" 返回结果: ", $result;

} else{

echo" 调用出错: ", $err;

}

//输出Hello, { hkshadow } !

Explanation:

Wsdl
WSDL is an XML language used to describe a Web service. It is a machine-readable format that provides all the information necessary to access a service to a Web service client. Nusoap specializes in providing a class for parsing WDSL files and extracting information from them. The SoapClient object uses a WSDL class to alleviate the difficulty of the developer invoking the service. With the help of the WSDL information to create the message, the programmer simply needs to know the name and parameters of the operation to invoke it.

Using WSDL with NUSOAP provides the following advantages:
All service meta-files, such as namespaces (namespaces), endpoint URLs, parameter names (parameter names), etc., can be obtained directly from the WSDL file, allowing the client to dynamically adapt to server-side changes. Because it is readily available from the server, this data no longer requires hard coding in user scripts.
It allows us to use the Soap_proxy class. This class derives from the SoapClient class and adds a method that corresponds to the operations detailed in the WDSL file. These methods can now be called directly by the user through it.
The SoapClient class contains a GetProxy () method that returns an object of a Soap_proxy class. The Soap_proxy class derives from the SoapClient class, adding a method that corresponds to the operation defined in the WSDL document, and allows the user to invoke a endpoint remote method. This applies only to cases where the SoapClient object is initialized with the Wdsl file. The advantage is that it is easy for users to use, and the disadvantage is that the creation of objects in performance –php is time-consuming – and is not serviced for 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

21st

//调用nusoap扩展类

$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');

//生成proxy类

$proxy= $client->getProxy();

//调用远程函数

$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 service-side url,http://127.0.0.1/test.php that you just created, and follow the results below.

Click the method name. This allows us to provide a visual document to the service by using Nusoap by adding a few lines of code to the service. But that's not all we can do.

We add some WSDL calls in the service by using NUSOAP we can generate WSDL for the service and some other documentation. In contrast, there are few things we can do in the client, at least in our simple example. The client shown below is no different from the client without WSDL, the only difference being that parsing soapclent class is done by providing the URL to the WSDL, not the previous service endpoint.

Nusoap call WebService when the encoding can be set, garbled solution 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 the call will report an error similar to the following:
XML error parsing SOAP payload on line x (row number): Reserved XML Name

If the PHP5 built-in soap is turned on, Nusoap's SoapClient class and PHP5 's built-in soap class are in conflict (I don't have a situation like this, 2 are open at the same time):
Solution Solutions
1. Modify php.ini does not load PHP5 built-in SOAP extensions (Windows is Php_soap.dll).
2. There is also a renaming of the SoapClient class for Nusoap.

At this point, whether it is php5 built-in soap or nusoap extension class, and for WebService is a solution, from the above examples are visible, nusoap appear more flexible, and for the simple webserice communication, PHP5 built-in soap is faster, regardless of which one you choose.

Original 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 describes the webservicephp soap and NUSOAP Server and client communication, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.