Simple PHP WebService implementation example and implementation step _ PHP Tutorial

Source: Internet
Author: User
Simple example and implementation steps for implementing WebService in PHP. PHP WebService implementation simple examples and implementation steps this article mainly introduces PHP WebService implementation simple examples and implementation steps, this article directly gives the sample code and step by step, simple example and implementation steps for WebService implementation 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:


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:


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:


$ 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:


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:


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

This article mainly introduces the simple example and implementation steps for PHP to implement WebService. This article provides the sample code and explains it step by step...

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.