Simple example and implementation steps for implementing WebService in PHP

Source: Internet
Author: User

Simple example and implementation steps for implementing WebService in PHP

This article mainly introduces the simple example and implementation steps for PHP to implement WebService. This article provides sample code and explains it in steps. For more information, see

 

 

Some time ago, I had a long struggle with the issue of webservice. I wanted to write it in the thinkphp framework, but it could not be implemented. So far, I have only learned how to Develop interfaces without frameworks.

To share the resource, follow these steps:

First, the files I created are:

Api. php api interface files

Api. wsdl

Cometrue. php registers the execution file of all the content of the service api class.

Creat_wsdl.php: Create the method file for executing the wsdl File

Soap files that must be called by SoapDiscovery. class. php (you can query and download them online)

Step 1: create a service folder to store your api Project

Step 2: Download The SoapDiscovery. class. php file and put it in your service folder.

Step 3: Create your own api file api. php under the service folder. The file content is as follows:

The Code is as follows:


<? Php
Class api {
// My test interface method
Public function test (){
Return "hello world ";
}
}
?>


Now, after writing our interface, we start to generate the wsdl file for this interface.

 

Part 4: Write an execution program that generates the wsdl file. Here I will create a new file creat_wsdl.php, which is also placed under the directory of the service level. The content is as follows:

The Code is as follows:


<? Php
Include ("api. php ");
Include ("SoapDiscovery. class. php ");
$ Disc = new SoapDiscovery ('api ', 'service'); // api class file name, service Interface directory
$ Disc-> getWSDL ();
?>

 

Step 5: Execute the creat_wsdl.php File

Then, an api. wsdl file is added to the service folder, as shown in:

Not yet complete ~

Step 6: register all methods in the api file. In the service folder, create a new registration file named "cometrue. php". The file content is as follows:

The Code is as follows:


<? Php
$ Server = new SoapServer ('api. wsdl ', array ('soap _ version' => SOAP_1_2); # The Service. wsdl file is generated above.
$ Server-> setClass ("api"); // register all methods of the Service class
$ Server-> handle ();
?>


After the file is created, execute the file and register all the content in the class file. Let's test this interface.

 

Step 7: add the called program at the bottom of the api class file api. php, and the content of the api. php file is as follows:

The Code is as follows:


<? Php
Class api {
// My test interface method
Public function test ($ ){
Return $;
}
}
$ Server = new SoapServer ('api. wsdl ', array ('soap _ version' => SOAP_1_2); # The Service. wsdl file is generated above.
$ Server-> setClass ("api"); // register all methods of the Service class
$ Server-> handle ();
?>

 

Step 7: test. Create a test file named test. php at any location outside the service (as long as the file can be accessed). The file content is as follows:

The Code is as follows:


<? Php
$ X = new SoapClient ("http://rbac.local/service/api.php? Wsdl "); // The link here is replaced by your own access Link
Echo $ x-> test ('OK ');
?>

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.