Create the standard webservice implementation code using wsdl in PHP

  • 2020-05-10 17:47:17
  • OfStack

1. Create wsdl
Description:
A, non-standard webservice, may only be accessible by PHP
B, webservice, wsdl(webservice description language, XML grammar standard to describe your service content, I understand)
I will only introduce the standard webservice here.
So how do you create wsdl? This is not an easy thing to do for PHP. Some people say it is very convenient to create zend studio. This is one way. But for those who don't like zend studio, it's too much to create an webservice and install zend studio. I'm like, hehe.
Here I introduce a simple method, go to the Internet to download SoapDiscovery.class.php class, there is a common method: getWSDL, this method is used at the end of return, so, you modify this method 1, I do it like this:
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' < /definitions > ');
// generate the wsdl file and comment the return above
$fso = fopen($this- > class_name . ".wsdl" , "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' < /definitions > '));
The class that generates wsdl now has SoapDiscovery.class.php ★.

I only need to prepare one more class or function to provide the service to create wsdl
For example, I have a class: person, the file name is: person.class.php ★, there are two methods, 1 is say, 1 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."); 
} 
} 
?> 

Here we have two classes: SoapDiscovery.class.php and person.class.php.
Start the formal generation of wsdl:
Create the file server.php. Copy the following and run to generate an person.wsdl file
 
<?php 
include("person.class.php"); 
include("SoapDiscovery.class.php"); 

$disco = new SoapDiscovery('person','Person');// The first 1 Two parameters are the class name (generated wsdl The file is named after it), i.e person Class, the first 2 Two parameters are the name of the service (you can write this as you like).  
$disco->getWSDL(); 
?> 

2. Create webservice server program
Empty the contents of the server.php file and copy the following code into it:
 
<?php 
include("person.class.php"); 
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl I just created it wsdl file  
//$objSoapServer = new SoapServer("server.php?wsdl");// This also line  
$objSoapServer->setClass("person");// registered person class  
$objSoapServer->handle();// Handle the request  
?> 

3. Create webservice client program and test whether webservice is valid. The file name is client.php
Copy in the following
 
<?php 
$client = new SoapClient("person.wsdl"); 
//$client = new SoapClient("server.php?wsdl");// This also line  
echo($client->say()); 
echo "<br />"; 
echo($client->run()); 
echo "<br />"; 
?> 

OK, end. Simple, right?
If you want to use NET, all you have to do is provide one url.
To get url: you can look it up in the person.wsdl file < soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" / > , url (the specific url is determined according to your directory) is what you provide to developers of NET. But don't get too excited. "? wsdl ", http: / / xxxxxxxxxxxxxxxxxxxx server php? This is true for wsdl. If you don't believe me, you can copy url to the address bar of your browser.
After the NET developer has obtained the url you gave him, he can add a service reference or an web reference to his own project, and then follow the instructions to do so, which is easy for developers using.NET.

Here I will only introduce the standard webservice
1. Create WSDL
1. SoapDiscovery.class.php
2. Modify getWsdl(), the public method for SoapDiscovery.class.php, to automatically generate the wsdl file (note the location path). Here, just create an wsdl model
 
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'); 
// generate wsdl File will be above return annotation  
$fso = fopen($this->class_name . ".wsdl" , "w"); 
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>')); 
exit; 

3. The class or function that provides the service
 
// Let's say I have a class: person , file name: person.class.php ★ there are two methods. 1 One is say . 1 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. Start the formal generation of wsdl:
Create file server.php. Copy the following and run to generate an person.wsdl file
 
<?php 
include("person.class.php"); 
include("SoapDiscovery.class.php"); 
// The first 1 Two parameters are the class name (generated wsdl The file is named after it), i.e person Class, the first 2 Two parameters are the name of the service (you can write this as you like).  
$disco = new SoapDiscovery('person','Person'); 
$disco->getWSDL(); 
?> 

5. Create the webservice server program
Empty the contents of the server.php file and copy the following code into it:
 
<?php 
include("person.class.php"); 
$objSoapServer = new SoapServer("person.wsdl");//person.wsdl I just created it wsdl file  
//$objSoapServer = new SoapServer("server.php?wsdl");// This also line  
$objSoapServer->setClass("person");// registered person class  
$objSoapServer->handle();// Handle the request  
?> 

6. Create the webservice client program and test whether webservice is valid. The file name is client.php
 
<?php 
$client = new SoapClient("person.wsdl"); 
//$client = new SoapClient("server.php?wsdl");// This also line  
echo($client->say()); 
echo "<br />"; 
echo($client->run()); 
echo "<br />"; 
?> 

7. If you want to use NET, all you have to do is provide him with an url.
Get url: you can look it up in the person.wsdl file < soap:address location="http://xxxxxxxxxxxxxxxxxxxx/server.php" / > , url (url is determined according to your directory) is what you provide to developers of NET. But don't get too excited. "? wsdl ", http: / / xxxxxxxxxxxxxxxxxxxx server php? This is true of wsdl. If you don't believe me, you can copy url into your browser's address bar.
After the NET developer has obtained the url you gave him, he can add a service reference or an web reference to his own project, and then follow the prompts to do so, which is easy for developers using.NET.

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

(2) power invocation
 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) { 
sdaf.Solsoft_HelloWorld ddd = new sdaf.Solsoft_HelloWorld(); 
Label1.Text = ddd.say(); 
} 
} 

The test code http: / / xiazai. ofstack. com / 201112 / yuanma CreateSoap. rar

Related articles: