Simple example and implementation steps for implementing WebService in PHP, webservice example
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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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 ');
?>