Using WSDL in PHP to create a standard WebService implementation code _php tutorial

Source: Internet
Author: User
Tags php class
1. Create WSDL
Description
A, non-standard webservice, can only be accessed by PHP
B, the standard webservice, you have to use the WSDL (WebService Description Language, is the XML syntax standard to describe your service content, I understand)
Here I only introduce the standard webservice.
So how do you create WSDL? This is a really hard thing for PHP, and some people say it's easy to create it with Zend Studio, which is a method. But for those who do not like to use Zend Studio, will feel to create a webservice also install Zend Studio, too much, I just, hey.
Here I introduce a simple method, to download the SoapDiscovery.class.php class online, there is a common method: Getwsdl, this method at the end of the return, then, you modify this method, I do this:
return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '');
Generate a WSDL file with the return comment above
$fso = fopen ($this->class_name. ". wsdl", "w");
Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
Now the class that generates the WSDL has, soapdiscovery.class.php★.

I just need to prepare a service class or function to create the WSDL.
For example, I have a class: person, file name: person.class.php★, there are two methods, one is say, one is run. Very simple.
Copy CodeThe code is as follows:
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 here: SoapDiscovery.class.php and person.class.php.
To begin the formal generation of WSDL:
Create the file server.php. Copy the following to run to generate a person.wsdl file
Copy CodeThe code is as follows:
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 after it), the person class, and the second parameter is the name of the service (this can be written casually).
$disco->getwsdl ();
?>

2. Create WebService Server program
Empty the contents of the server.php file and copy the following code in:
Copy CodeThe code is as follows:
Include ("person.class.php");
$objSoapServer = new SoapServer ("person.wsdl");//person.wsdl is the WSDL file that was just created
$objSoapServer = new SoapServer ("server.php?wsdl");//This is OK
$objSoapServer->setclass ("person");//All methods of registering the person class
$objSoapServer->handle ();//Processing request
?>

3, create WebService client program, test webservice is valid, file name is: client.php
Copy the following contents in the
Copy CodeThe code is as follows:
$client = new SoapClient ("person.wsdl");
$client = new SoapClient ("server.php?wsdl");//This is OK
Echo ($client->say ());
echo "
";
Echo ($client->run ());
echo "
";
?>

OK, end. Very simple, huh?
. NET if you want to use, you just have to provide a URL to him on the line.
How to get the URL: You can find it in the person.wsdl file first , here the URL (the specific URL is determined by your directory) is what you want to provide. NET developers to use. But do not be happy too early, the following to add: "? wsdl", http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl this is the right, do not believe you can copy the URL to the browser's address bar to see it.
. NET developer gets the URL you gave him, you can add a service reference or Web reference to your project, and then you can follow the prompts to complete the operation, for use. NET developers are simple.

here I only introduce the standard webservice
First, create WSDL
1. Download SoapDiscovery.class.php class online
2. Modify the public method of SoapDiscovery.class.php getwsdl () to automatically generate the WSDL file (note the storage path), here just create a WSDL model
Copy CodeThe code is as follows:
return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '');
Generate a WSDL file with the return comment above
$fso = fopen ($this->class_name. ". wsdl", "w");
Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
Exit

3. The class or function that provides the service
Copy CodeThe code is as follows:
For example, I have a class: person, file name: person.class.php★, there are two methods, one is say, one is run. Very simple.
Class Person
{
Public function Say ()
{
Return ("I ' m speaking.");
}
Public Function Run ()
{
Return ("I ' m running,don ' t disturb me please.");
}
}
?>

4. To begin the formal generation of WSDL:
Create the file server.php. Copy the following to run to generate a person.wsdl file
Copy CodeThe code is as follows:
Include ("person.class.php");
Include ("SoapDiscovery.class.php");
The first parameter is the class name (the generated WSDL file is named after it), the person class, and the second parameter is the name of the service (this can be written casually).
$disco = new Soapdiscovery (' person ', ' person ');
$disco->getwsdl ();
?>

5. Create a WebService service-side program
Empty the contents of the server.php file and copy the following code in:
Copy CodeThe code is as follows:
Include ("person.class.php");
$objSoapServer = new SoapServer ("person.wsdl");//person.wsdl is the WSDL file that was just created
$objSoapServer = new SoapServer ("server.php?wsdl");//This is OK
$objSoapServer->setclass ("person");//All methods of registering the person class
$objSoapServer->handle ();//Processing request
?>

6. Create the WebService client program to test whether the webservice is valid and the file name is: client.php
Copy CodeThe code is as follows:
$client = new SoapClient ("person.wsdl");
$client = new SoapClient ("server.php?wsdl");//This is OK
Echo ($client->say ());
echo "
";
Echo ($client->run ());
echo "
";
?>

7.. NET if you want to use, you just have to provide a URL to him on the line.
How to get the URL: You can find it in the person.wsdl file first , here the URL (the specific URL is determined by your directory) is what you want to provide. NET developers to use. But do not be happy too early, the following to add: "? wsdl", http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl this is the right, do not believe you can copy the URL to the browser's address bar to see it.
. NET developer gets the URL you gave him, you can add a service reference or Web reference to your project, and then you can follow the prompts to complete the operation, for use. NET developers are simple.

(1) Create a website, create a Web reference, enter a URL

(2) Strength call
Copy 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 ();
}
}

Test Code Http://xiazai.jb51.net/201112/yuanma/CreateSoap.rar

http://www.bkjia.com/PHPjc/324674.html www.bkjia.com true http://www.bkjia.com/PHPjc/324674.html techarticle 1, create WSDL Description: A, non-standard WebService, may only PHP to access B, the standard webservice, you must use the WSDL (WebService Description Language, is the XML syntax ...

  • 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.