Writing API in php here actually refers to providing a WebService
WebSite : 1.以html格式响应返回 2.由用户通过浏览器来接入 WebService : 1.以json/Xml格式返回 2.由程序接入 3.用来实现不同平台的交互
How to begin
To put it simply, it is to transmit data and encapsulate various methods for others to call
A simple example
How to Write an interface for student meal cards?
As an intermediate layer, we need to get the user's data (i.e. student's student ID number, student card password), and know where we should POST the data (such as www.123.com/Api/login), and understand Data format, we can start work.
The specific process is that after the user enters the username and password into the form, we receive the username and password, and then write them into the required data format, such as username="&password=", and then interact with the server through curl
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_all); $data = curl_exec($ch); curl_close($ch);
This It is some key interaction code, among which$fields_all represents a part of a URL request written according to the data format and encoded by urlencode. And$fields represents the name of each data variable in$fields_all
The server will then have a returned json data, and it will be clear at a glance whether the data has been successfully interacted
Of course, this is just the simplest interaction, and there may be more When you encounter other confusing logic, you can only think about it slowly. In fact, those who are used to writing websites will still think that writing APIs is quite simple, but there is no sense of accomplishment...
Of course, this is not based on What is the restful style? The next article will briefly talk about the rest experience... Of course, it is also very exciting to use Yii and Laravel to develop with the framework. After all, others support rest..
Written by a pure novice, light spray.
--by vampirebitter
The above introduces Xiaobai's first experience with PHP API, including api and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.