This article mainly introduces simple examples and implementation steps for PHP to implement WebService. This article directly gives the sample code and explains it step by step. , friends in need can refer to it
Some time ago, I struggled with the issue of webservice for a long time. I originally wanted to write it in the thinkphp framework, but it couldn't be realized. So far, I have only learned how to develop interfaces without a framework.
Share the steps in this resource:
First the files I created are:
api.php api interface class file
api.wsdl The interface file I created to be called at the end
cometrue.php is the execution file that registers all content of the service api class
creat_wsdl.php creates the execution method file of wsdl file
SoapDiscovery.class.php is the soap class file that must be called (can be queried and downloaded online)
Step one: Create a service folder specifically to store your api interface project
Step 2: Download the SoapDiscovery.class.php class file and place it in your service folder
Step 3: Create your own api interface class file api.php under the service folder. An example of the file content is as follows:
The code is as follows:
Part 4: Write an execution program that generates wsdl files. Here I will create a new file creat_wsdl.php, which is also placed under the directory of the same level as service. The content is as follows:
The code is as follows:
Step 5: Execute the creat_wsdl.php file
Then, there is an api.wsdl file under the service folder, as shown in the figure below:
It’s not over yet~
Step 6: Register all methods in the api class file. Create a new executable file of the registration class file under the service folder and name it: cometrue.php. The file content is as follows:
The code is as follows:
Step 7: At the bottom of the api class file api.php, add the calling program, and then add the api.php file content as follows:
The code is as follows:
Step 7: Test, create a test file in a random location outside the service (as long as it can be accessed) and name it: test.php. The content of the file is as follows:
The code is as follows: