Home > Article > Backend Development > Create standard webservice_PHP tutorial using wsdl in PHP
Description:
• Non-standard webservice may only be accessible through PHP
• For standard webservice, you must use wsdl
Here I only introduce the standard webservice www.2cto.com
1. Create WSDL
1. Download the SoapDiscovery.class.php class online
2. Modify the public method getWsdl() of SoapDiscovery.class.php to automatically generate a wsdl file (note the storage path). Here we just create a wsdl model
1 //return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '');
2 // Generate wsdl file, comment the above return
3 $fso = fopen($this->class_name . ".wsdl" , "w");
4fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
5 exit;
3. Class or function that provides services
1 //For example, I have a class: person, the file name is: person.class.php★, there are two methods in it, one is say and the other is run. Very simple.
2
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 formally generating wsdl:
Create the file server.php. Copy the following content into it and run it to generate a person.wsdl file
1
2 include("person.class.php");
3 include("SoapDiscovery.class.php");
4 //The first parameter is the class name (the generated wsdl file is named after it), which is the person class, and the second parameter is the name of the service (you can write this casually).
5 $disco = new SoapDiscovery('person','Person');
6 $disco->getWSDL();
7 ?>
5. Create webservice server program
Clear the contents of the server.php file and copy the following code into it:
1
2 include("person.class.php");
3 $objSoapServer = new SoapServer("person.wsdl");//person.wsdl is the wsdl file just created
4 //$objSoapServer = new SoapServer("server.php?wsdl");//This will also work
5 $objSoapServer->setClass("person");//Register all methods of the person class
6 $objSoapServer->handle();//Processing the request
7 ?>
6. Create a webservice client program to test whether the webservice is valid. The file name is: client.php
$client = new SoapClient("person.wsdl");
//$client = new SoapClient("server.php?wsdl");//This also works
echo($client->say());
echo "
";
echo($client->run());
echo "
";
?>
7. If you want to use .NET, you just need to provide a URL to it.
How to get the URL: You can first search for
After a .NET developer gets the URL you gave him, he can add a service reference or web reference to his project, and then he can complete the relevant operations according to the prompts. It is very simple for developers using .NET.
(1) Create a website, create a web reference, enter url
(2) Strength 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
2011-12-07 21:37:56
Author Dim