Use wsdl in PHP to create a standard webservice

Source: Internet
Author: User
Tags php online

 

Note:

• Non-standard webservice, which can only be accessed by PHP

• Standard webservice requires the use of wsdl

Here I will only introduce the standard webservice www.2cto.com

1. Create A WSDL

1. Download SoapDiscovery. class. php online

2. Modify the public method getWsdl () of SoapDiscovery. class. php to automatically generate the wsdl file (note the storage path). Here we only create a wsdl model.

1 // return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> ');

2 // generate the wsdl file and comment out the above return

3 $ fso = fopen ($ this-> class_name. ". wsdl", "w ");

4 fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> '));

5 exit;

3. Classes or functions that provide services

1 // For example, I have a class: person, and the file name is person. class. php.★There are two methods, one is say and the other is run. Very simple.

2 <? Php

3 class person

4 {

5 public function say ()

6 {

7 return ("I'm speaking .");

8}

9 public function run ()

10 {

11 return ("I'm running, don't disturb me please .");

12}

13}

14?>

4. Start to formally generate the wsdl:

Create the file server. php. Copy the following content and run the command to generate a person. wsdl file.

1 <? Php

2 include ("person. class. php ");

3 include ("SoapDiscovery. class. php ");

4 // The first parameter is the class name (the generated wsdl file is named by it), that is, the person class, and the second parameter is the service name (which can be written at will ).

5 $ disco = new SoapDiscovery ('person ', 'person ');

6 $ disco-> getWSDL ();

7?>

5. Create a webservice server program

Clear the content of the server. php file and copy the following code:

1 <? Php

2 include ("person. class. php ");

3 $ objSoapServer = new SoapServer ("person. wsdl"); // person. wsdl is the newly created wsdl file.

4 // $ objSoapServer = new SoapServer ("server. php? Wsdl "); // This is also true

5 $ objSoapServer-> setClass ("person"); // register all methods of the person class

6 $ objSoapServer-> handle (); // process the request

7?>

6. Create a webservice client program and test whether the webservice is valid. The file name is client. php.

<? Php

$ Client = new SoapClient ("person. wsdl ");

// $ Client = new SoapClient ("server. php? Wsdl "); // This is also true

Echo ($ client-> say ());

Echo "<br/> ";

Echo ($ client-> run ());

Echo "<br/> ";

?>

7 .. NET, you only need to provide a url for it.

Method to obtain the url: You can first go to person. find <soap: address location = "http: // xxxxxxxxxxxxxxxxxxxx/server. php "/>, the url here (the specific url is determined based on your directory) is what you want to provide.. NET developer. But don't be too happy. You need to add "? Wsdl ", http: // xxxxxxxxxxxxxxxxxxxx/server. php? Wsdl is correct. If you do not believe it, you can copy the url to the address bar of the browser and check it.

. NET developers can add a service reference or web reference in their own projects after obtaining the url you have given them. Then, they can complete related operations as prompted. NET developers.

(1) create a website, create a web reference, and enter the url

(2) powerful call

Protected void Page_Load (object sender, EventArgs e)

{

If (! IsPostBack ){

Sdaf. Solsoft_HelloWorld ddd = new sdaf. Solsoft_HelloWorld ();

Label1.Text = ddd. say ();

}

}

Attachment download: http://www.bkjia.com/uploadfile/2011/1208/20111208050034814.rar

 

21:37:56

Pale author

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.