Writing API in php here actually refers to providing a WebService
<code>WebSite : <span>1.</span>以html格式响应返回 <span>2.</span>由用户通过浏览器来接入 WebService : <span>1.</span>以json/<span>Xml</span>格式返回 <span>2.</span>由程序接入 <span>3.</span>用来实现不同平台的交互</code>
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
<code><span>$ch</span> = curl_init(); curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>$url</span>); curl_setopt(<span>$ch</span>, CURLOPT_POST, count(<span>$fields</span>)); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span>); curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$fields_all</span>); <span>$data</span> = curl_<span>exec</span>(<span>$ch</span>); curl_close(<span>$ch</span>);</code>
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.
<code> --by vampirebitter </code>
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.