Using WSDL in PHP to create standard WebService implementation code _php tips

Source: Internet
Author: User
Tags soap php class sprintf zend wsdl
1. Create WSDL
Description
A, non-standard webservice, may only be able to access PHP
B, the standard webservice, you have to use the WSDL (WebService description Language, is to use the XML syntax standard to describe your service content, I understand so)
Here I only introduce the standard webservice.
So how do you create WSDL? This is really not easy for PHP, and some people say it's convenient to create it with Zend Studio, which is a way of doing it. But for those who do not like to use Zend Studio, it will feel that creating a webservice to install Zend Studio, too pushy, 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 did so:
return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> ');
Generate the WSDL file and add 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, ' </ Definitions> '));
Now the class that generates the WSDL has, soapdiscovery.class.php★.

I just need to prepare a class or function that provides the service 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 Code code as follows:

<?php
Class Person
{
Public function Say ()
{
Return ("I ' m speaking.");
}
Public Function Run ()
{
Return ("I ' m running,don ' t disturb me please.");
}
}
?>

Here are two classes: SoapDiscovery.class.php and person.class.php.
Begin the formal generation of WSDL:
Create a file server.php. Copy the following and run to generate a person.wsdl file
Copy Code code as follows:

<?php
Include ("person.class.php");
Include ("SoapDiscovery.class.php");

$disco = new Soapdiscovery (' persons ', ' person ');//The first argument is the class name (the generated WSDL file is named for 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 Code code as follows:

<?php
Include ("person.class.php");
$objSoapServer = new SoapServer ("person.wsdl");//person.wsdl is the WSDL file you just created
$objSoapServer = new SoapServer ("server.php?wsdl");/this will do.
$objSoapServer->setclass ("person");//Register all methods of the person class
$objSoapServer->handle ();//Processing request
?>

3, create WebService client program, test webservice is valid, the filename is: client.php
Copy the following content in
Copy Code code as follows:

<?php
$client = new SoapClient ("person.wsdl");
$client = new SoapClient ("server.php?wsdl");/this will do.
Echo ($client->say ());
echo "<br/>";
Echo ($client->run ());
echo "<br/>";
?>

OK, end. It's simple, isn't it?
. NET if you want to use, you just provide a URL to him on the line.
The way to get the URL: You can go to the person.wsdl file to find <soap:address location= "http://xxxxxxxxxxxxxxxxxxxx/server.php"/&GT; The URL here (specific URL is based on your directory is determined) is what you want to provide. NET developer to use. But don't be happy too early, then 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 what you know.
. 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 relevant actions for use. NET developer, it's very simple.

I'll just introduce the standard webservice here.
First, create WSDL
1. Download SoapDiscovery.class.php class online
2. Modify the SoapDiscovery.class.php public Method getwsdl () to automatically generate a WSDL file (note that the path is stored), just create a WSDL model
Copy Code code as follows:

return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> ');
Generate the WSDL file and add 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, ' </ Definitions> '));
Exit

3. A class or function that provides a service
Copy Code code 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.
<?php
Class Person
{
Public function Say ()
{
Return ("I ' m speaking.");
}
Public Function Run ()
{
Return ("I ' m running,don ' t disturb me please.");
}
}
?>

4. Begin the formal generation of WSDL:
Create a file server.php. Copy the following and run to generate a person.wsdl file
Copy Code code as follows:

<?php
Include ("person.class.php");
Include ("SoapDiscovery.class.php");
The first parameter is the class name (the generated WSDL file is named for 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. Creating WebService server-side programs
Empty the contents of the server.php file and copy the following code in:
Copy Code code as follows:

<?php
Include ("person.class.php");
$objSoapServer = new SoapServer ("person.wsdl");//person.wsdl is the WSDL file you just created
$objSoapServer = new SoapServer ("server.php?wsdl");/this will do.
$objSoapServer->setclass ("person");//Register all methods of the person class
$objSoapServer->handle ();//Processing request
?>

6. Create WebService client program, test webservice is valid, file name is: client.php
Copy Code code as follows:

<?php
$client = new SoapClient ("person.wsdl");
$client = new SoapClient ("server.php?wsdl");/this will do.
Echo ($client->say ());
echo "<br/>";
Echo ($client->run ());
echo "<br/>";
?>

7.. NET if you want to use, you just provide a URL to him on the line.
The way to get the URL: You can go to the person.wsdl file to find <soap:address location= "http://xxxxxxxxxxxxxxxxxxxx/server.php"/&GT; The URL here (specific URL is based on your directory is determined) is what you want to provide. NET developer to use. But don't be happy too early, then 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 what you know.
. 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 relevant actions for use. NET developer, it's very simple.

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

(2) Power call
Copy Code code 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

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.