PHP soap instance description

Source: Internet
Author: User

I. What is soap, What Is WSDL, and why should I use them?

Soap is a language supported by various languages based on XML and HTTP communication protocols and XML platforms. HTTP is supported by all Internet browsers and servers.

The Web Service Description Language (WSDL) is a document written in XML. This document describes a web service. It specifies the location of the service and the operations it provides.

I am a PHP developer, you are a Java developer, and he is a. Net developer. What should I do if we need to communicate with each other and exchange data? We need a tool that can communicate with us. Soap and WSDL are created so that applications running on different operating systems and using different technologies and programming languages can communicate with each other.

Ii. Instances

If php wants to use soap, the common practice is to add the PHP soap module. add soap in ini. so. Next we will introduce a method that does not add soap. so file, you can also implement the soap Method

Nusoap is a functional file written by PHP. It can be used when it comes in. You can search for it yourself on the Internet.

1. Do not use WSDL

A. Server helloworld2.php

<? Php <br/> // package nusoap. php <br/> require_once ('. /lib/nusoap. PHP '); </P> <p> // create a server <br/> $ Server = new soap_server; </P> <p> // define the client call method <br/> $ server-> Register ('hello '); </P> <p> // call method and parameters <br/> function Hello ($ name) {<br/> return 'hello ,'. $ name; <br/>}</P> <p> $ http_raw_post_data = isset ($ http_raw_post_data )? $ Http_raw_post_data: ''; <br/> $ server-> service ($ http_raw_post_data); <br/>?>

B. Client Hello. php

<? Php <br/> // package nusoap. php <br/> require_once ('. /lib/nusoap. PHP '); <br/> // create a soap client and call the WSDL provided by the server <br/> // $ client = new soapclient ('HTTP: // localhost/test/hellowsdl2.php? WSDL ', true); <br/> $ client = new soapclient ('HTTP: // localhost/test/helloworld2.php '); <br/> // check whether an error is returned. <br/> $ err = $ client-> geterror (); <br/> if ($ ERR) {<br/> // Display error <br/> echo '<H2> constructor error </H2> <PRE> '. $ err. '</PRE> '; <br/>}</P> <p> // call the server method <br/> $ result = $ client-> call ('hello ', array ('person '=> "this is a test"); </P> <p> echo' <H2> result </H2> <PRE> '; <br/> print_r ($ result); <br/> echo' </PRE> '; <br/>?>

2. Use WSDL

A. Server Side

<? Php <br/> // package nusoap. php <br/> require_once ('. /lib/nusoap. PHP '); <br/> // create a soap service <br/> $ Server = new soap_server (); <br/> // initialization supports WSDL <br/> $ server-> configurewsdl ('hellowsdl2 ', 'urn: hellowsdl2 '); <br/> // define a Data Structure to receive data <br/> $ server-> WSDL-> addcomplextype (<br/> 'person ', <br/> 'complextype', <br/> 'struct', <br/> 'all', <br/> '', <br/> array (<br/> 'firstname' => array ('name' => 'firstname', 'type' => 'xsd: String '), // The type following it defines the data type. This is string <br/> 'age' => array ('name' => 'age ', 'type' => 'xsd: int'), // The type following it defines the data type, this is int <br/> 'gender' => array ('name' => 'gender', 'type' => 'xsd: string ') // The following type defines the data type, which is string <br/> ); <br/> $ server-> WSDL-> addcomplextype (<br/> 'sweepstakesgreeting ', <br/> 'complextype', <br/> 'struct ', <br/> 'all', <br/> '', <br/> array (<br/> 'greeting '=> array ('name' => 'Greeting ', 'type' => 'xsd: string'), <br/> 'winner' => array ('name' => 'winner ', 'type' => 'xsd: string') <br/> ); <br/> // soap call method defined by the server <br/> $ server-> Register ('hello', // Method Name: Hello, the method is as follows <br/> array ('person '=> 'tns: person '), // variables sent from the client <br/> array ('Return '=> 'tns: sweepstakesgreeting'), // return parameters <br/> 'urn: hellowsdl2 ', // soap name <br/> 'urn: hellowsdl2 # hello', // soap method name <br/> 'rpc ',// <Br/> 'encoded', // encoding <br/> 'test' // archive <br/> ); <br/> // define the previously registered function Hello <br/> function Hello ($ person) {<br/> $ greeting = 'hello ,'. $ person ['firstname']. <br/> '. it is nice to meet '. $ person ['age']. <br/> 'year old '. $ person ['gender']. '. '; </P> <p> $ Winner = 'Scott '; <br/> // data to be returned <br/> return array (<br/> 'greeting '=> $ greeting, <br/> 'winner' = >$ winner <br/>); <br/>}< br/>/ /When a request (attempt) calls the service <br/> $ http_raw_post_data = isset ($ http_raw_post_data )? $ Http_raw_post_data: ''; <br/> $ server-> service ($ http_raw_post_data); <br/>?>

B. Client

<? Php <br/> // package nusoap. php <br/> require_once ('. /lib/nusoap. PHP '); <br/> // create a soap client and call the WSDL provided by the server <br/> // $ client = new soapclient ('HTTP: // localhost/test/hellowsdl2.php? WSDL ', true); <br/> $ client = new soapclient ('HTTP: // localhost/test/helloworld2.php '); <br/> // check whether an error is returned. <br/> $ err = $ client-> geterror (); <br/> if ($ ERR) {<br/> // Display error <br/> echo '<H2> constructor error </H2> <PRE> '. $ err. '</PRE> '; <br/>}< br/> // parameters to be passed to the server <br/> $ person = array ('firstname' => 'willi ', 'age' => 22, 'gender' => 'male '); </P> <p> // call the server method <br/> $ result = $ client-> call ('hello', array ('pers On '=> $ person); <br/> // error review <br/> if ($ client-> fault) {<br/> echo '<H2> fault </H2> <PRE>'; <br/> print_r ($ result ); <br/> echo '</PRE>'; <br/>} else {<br/> $ err = $ client-> geterror (); <br/> if ($ ERR) {<br/> echo '<H2> error </H2> <PRE> '. $ err. '</PRE>'; <br/>} else {<br/> echo '<H2> result </H2> <PRE> '; <br/> print_r ($ result); <br/> echo '</PRE> '; <br/>}< br/> // display Request Information <br/> echo '<H2> request </H2> '; <br/> echo' <PRE> '. htmlspecialchars ($ client-> request, ent_quotes ). '</PRE>'; <br/> // display the returned information <br/> echo '<H2> response </H2> '; <br/> echo '<PRE> '. htmlspecialchars ($ client-> response, ent_quotes ). '</PRE>'; <br/> // display debugging information <br/> echo '<H2> debug </H2> '; <br/> echo '<PRE> '. htmlspecialchars ($ client-> debug_str, ent_quotes ). '</PRE>'; <br/>?>

In the above two examples, both the client and the server are written in PHP. You can try writing in multiple languages to test it. No matter whether you use the PHP module or nusoap, the specific method is not mentioned here, and the Manual contains it.

Soap is not introduced here. Here is just a simple implementation of a soap instance. Let's just look at the code. Soap is divided into server and client. We want the client to call the server code. First, let's look at the server short code:

This is the server code: Server. php

<? Php </P> <p> // declare a function add () and return its value <br/> function add ($ A, $ B) {<br/> return $ A + $ B; <br/>}</P> <p> // instantiate a soapserver object, and register the Add function as its method <br/> $ Server = new soapserver (null, array ('uri '=> 'HTTP: // localhost /')); // specify the URI (resource identifier) of the server code <br/> $ server-> addfunction ("add"); <br/> $ server-> handle (); <br/>?>
Then use the client code to call the server code: the client code is also very simple:

This is the client code client. php.

<? Php </P> <p> // create a parameter array, store the address and program of the computer that provides the soap service to be accessed <br/> $ arroptions = array (<br/> 'uri' => 'HTTP: // localhost /', <br/> 'location' => 'HTTP: // localhost/soap/server. PHP ', // Note: This location specifies the specific location of the server-side code on the server. My location is in the soap directory under the local root directory, <br/> 'track' => true, <br/>); <br/> $ soapobject = new soapclient (null, $ arroptions ); // instantiate the client object <br/> echo $ soapobject-> Add (20, 30); // call the server-side function add and return a value of 50 <br/>?>
OK, it's over!

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.