Soap application in php

Source: Internet
Author: User
Tags soap client
SOAP: Simple Object Access Protocol (SOAP: SimpleObjectAccessProtocol) Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol, it is designed to exchange structured and solidified information on the WEB. SOAP can be used in combination with many existing Internet protocols and formats, including hypertext transfer protocols.

SOAP: Simple Object Access Protocol (SOAP) is a lightweight, Simple, XML-based Protocol, it is designed to exchange structured and solidified information on the WEB. SOAP can be used in combination with many existing Internet protocols and formats, including hypertext transfer protocols.

SOAP: Simple Object Access Protocol

(SOAP: Simple Object Access Protocol)

Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol designed to exchange structured and solidified information on the WEB. SOAP can be used in combination with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), and multi-purpose Internet Mail Extension protocol (MIME ). It also supports a large number of applications from the message system to Remote Process calling (RPC.

The following is a simple example of the application of soap in php.

1. Use php to provide the soap interface to others

To use php to provide the soap interface, the only thing we need to do is to use the SoapServer class to declare an interface.

The following is the sample code:

Php file: SoapInterfaceProvider. php

<? Php/*** interface to be provided to the user **/function getSoapData () {$ data = '<? Xml version = "1.0" encoding = "utf8"?> <Data> hello world <data> '; return $ data;} // The first parameter represents the wsdl, and the second parameter uri represents the namespace $ soap = new SoapServer (null, array ('uri '=> 'HTTP: // www.qqstore.net/soap/'); // declare the interface for the user to call $ soap-> addFunction ("getSoapData "); $ soap-> handle ();?>

2. Call the soap interface already provided in php

To test the interface provided in step 1, you only need to execute the following test code. (You only need to execute the following code without starting or executing it in advance.

SoapInterfaceProvider. php fileWhen the following test code is called, php will automatically execute the SoapInterfaceProvider. php file. So

The location parameter here is very important and cannot be an error. It must be the location where SoapInterfaceProvider. php is actually placed)

Php file: SoapClient. php

<? Php/* creates a Soap client. The first parameter represents the wsdl, and the second parameter represents the location of the php file defining the SoapServer. The uri parameter represents the location found in the namespace test, the uri here is even with SoapInterfaceProvide. php does not have to be defined differently. It may be related to my only soap interface. */$ Client = new SoapClient (null, array ('location' => 'HTTP: // localhost/webcenter/soap/SoapInterfaceProvider. php ', 'uri' => 'HTTP: // www.qqstore.net/soap/'); // call the getSoapData interface $ data = $ client-> getSoapData (); echo "to obtain data: ". $ data;?>

Enter the following connection in the browser for testing: http: // localhost/soap/SoapClient. php (adjust according to the actual location)

Execution result:

Obtained data: Hello world

3. code optimization

There is only one method in the file SoapInterfaceProvider. php of the soap interface provided above. If there are many methods to be provided, each callAddFunctionIt seems a little troublesome. It is not appropriate to write all the actual methods in the SoapInterfaceProvider. php file.

Here we can put all the actually called methods into a separate php class and useSetClassThe method declares all interfaces at a time.

The following is the sample code:

Php file: SoapMethod. class. php

<? Phpclass SoapMethod {/*** interface to be provided to the user **/function getSoapData () {$ data = '<? Xml version = "1.0" encoding = "UTF-8"?> <Data> hello world <data> '; return $ data;}/*** interface 1 **/function getSoapData1 () to be provided to the user () {$ data1 = '<? Xml version = "1.0" encoding = "UTF-8"?> <Data> hello world <data> '; return $ data1 ;}}?>

Php file: SoapInterfaceProvider. php

<? Php // introduce the require_once class containing the actually called method '. /function. class. php '; // The first parameter represents the wsdl, and the second parameter uri parameter represents the namespace $ soap = new SoapServer (null, array ('url' => 'HTTP: // www.qqstore.net/soap/'); // declare the interface for the user to call $ soap-> setClass ("SoapMethod"); $ soap-> handle ();?>

During the test, I didn't use the setClass method at first, but used the addFunction method. The actual called method was also put in the SoapMethod class, and

The above SoapMethod. class. php code is the same. The following error occurs when the addFunction method is called:

Fatal error: Uncaught SoapFault exception: [HTTP] Not Found in C:/AppServ/www/webcenter/soap/SoapClient. php: 4 Stack trace: #0 [internal function]: SoapClient->__ doRequest (' _ Call ('getsoapdata', Array) #2 C:/AppServ/www/webcenter/soap/SoapClient. php (4): SoapClient-> getSoapData () #3 {main} thrown inC:/AppServ/www/webcenter/soap/SoapClient. phpOn line4

The method is defined in the class and cannot be found in the SoapInterfaceProvider. php file. Remove the class definition in SoapMethod. class. php and retain

Method.

If the location parameter entered during SoapClient creation is incorrect, the preceding error occurs.

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.