Home  >  Article  >  Backend Development  >  Create standard webservice_PHP tutorial using wsdl in PHP

Create standard webservice_PHP tutorial using wsdl in PHP

WBOY
WBOYOriginal
2016-07-13 17:47:34931browse

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");

4​​​​fwrite($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 in the person.wsdl file. The URL here (the specific URL is determined based on your directory) ) is what you want to provide to .NET developers. But don’t get too happy too early. You need to add: “?wsdl” after it, http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl. This is correct. If you don’t believe me, you can copy the url to the address bar of the browser and take a look. .

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478495.htmlTechArticleNote: For non-standard webservices, only PHP can access standard webservices, so you must use wsdl here I only introduce the standard webservice www.2cto.com 1. Create WSDL...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn