Use wsdl in PHP to create standard webservice implementation code

Source: Internet
Author: User
Tags php online

1. Create A wsdl
Note:
A. Non-standard webservice, which can only be accessed by PHP
B. The standard webservice must use wsdl (webservice description language, which is used to describe your service content in XML syntax standards, which I understand)
Here I will only introduce the standard webservice.
How to Create A wsdl? For PHP, this is really not easy. Some people say that it is very convenient to create with zend studio. This is a method. But for those who do not like zend studio, they will feel that it is too difficult to create a webservice and install zend studio. I am, hey.
Here I will introduce a simple method to download SoapDiscovery online. class. php class, which has a public method: getWSDL. The return is used at the end of this method. So, you can modify this method. I did this:
// Return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> ');
// Generate the wsdl file and comment out the above return
$ Fso = fopen ($ this-> class_name. ". wsdl", "w ");
Fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> '));
Now the wsdl class is generated. SoapDiscovery. class. php★.

I only need to prepare a service-providing class or function to create the wsdl.
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.Copy codeThe Code is as follows: <? Php
Class person
{
Public function say ()
{
Return ("I'm speaking .");
}
Public function run ()
{
Return ("I'm running, don't disturb me please .");
}
}
?>

There are two classes: SoapDiscovery. class. php and person. class. php.
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.Copy codeThe Code is as follows: <? Php
Include ("person. class. php ");
Include ("SoapDiscovery. class. php ");

$ Disco = new SoapDiscovery ('person ', 'person'); // The first parameter is the class name (the generated wsdl file is named by it), that is, the person class, the second parameter is the service name (which can be written as needed ).
$ Disco-> getWSDL ();
?>

2. Create a webservice server program
Clear the content of the server. php file and copy the following code:Copy codeThe Code is as follows: <? Php
Include ("person. class. php ");
$ ObjSoapServer = new SoapServer ("person. wsdl"); // person. wsdl is the newly created wsdl file.
// $ ObjSoapServer = new SoapServer ("server. php? Wsdl "); // This is also true
$ ObjSoapServer-> setClass ("person"); // register all methods of the person class
$ ObjSoapServer-> handle (); // process the request
?>

3. Create a webservice client and test whether the webservice is valid. The file name is client. php.
Copy the following contentCopy codeThe Code is as follows: <? 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/> ";
?>

OK. Is it easy?
To use. 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.

Here I will only introduce the standard webservice.
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.Copy codeThe Code is as follows: // return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> ');
// Generate the wsdl file and comment out the above return
$ Fso = fopen ($ this-> class_name. ". wsdl", "w ");
Fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> '));
Exit;

3. Classes or functions that provide servicesCopy codeThe Code is as follows: // 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.
<? Php
Class person
{
Public function say ()
{
Return ("I'm speaking .");
}
Public function run ()
{
Return ("I'm running, don't disturb me please .");
}
}
?>

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.Copy codeThe Code is as follows: <? Php
Include ("person. class. php ");
Include ("SoapDiscovery. class. php ");
// 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 ).
$ Disco = new SoapDiscovery ('person ', 'person ');
$ Disco-> getWSDL ();
?>

5. Create a webservice server program
Clear the content of the server. php file and copy the following code:Copy codeThe Code is as follows: <? Php
Include ("person. class. php ");
$ ObjSoapServer = new SoapServer ("person. wsdl"); // person. wsdl is the newly created wsdl file.
// $ ObjSoapServer = new SoapServer ("server. php? Wsdl "); // This is also true
$ ObjSoapServer-> setClass ("person"); // register all methods of the person class
$ ObjSoapServer-> handle (); // process the request
?>

6. Create a webservice client program and test whether the webservice is valid. The file name is client. php.Copy codeThe Code is as follows: <? 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 callCopy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack ){
Sdaf. Solsoft_HelloWorld ddd = new sdaf. Solsoft_HelloWorld ();
Label1.Text = ddd. say ();
}
}

Code http://xiazai.jb51.net/201112/yuanma/CreateSoap.rar

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.