Home >Backend Development >PHP Problem >How to write php application interface
How to write the php application interface
The running of the php program requires the installation of the corresponding php operating environment, we can use phpStudy integration package for deployment, or you can use a virtual host that supports php to run php programs.
Here we take the virtual host as an example to explain how PHP writes interfaces for our applications.
Materials:
Support php virtual host.
Specific implementation:
1. Create a new php program
First, we create a new php File, if you don’t have a PHP development environment, use Notepad to create a new document, then directly enter a piece of content you want to return, then save it, rename it, and change the suffix to .php. Finally, throw it to the virtual host, as shown below:
Then we can directly access it with a browser and enter the address: http://ip/test.php, if you have The domain name has been resolved to the virtual host and can be accessed using http://domain name/test.php. Because it is in the Web directory, it can be opened directly by adding the IP address or the name of the file after the domain name.
2. PHP handles get requests
<?php $x = 5; $y = $_GET['id']; $z = $x + $y; echo "变量z为: $z"; ?>
The above code is to get the value of the id on the link, then add it to the value of x, and then return.
3. The application initiates a request (examples of requests initiated by Android and IOS can be found online)
##ok, just as we thought, the content was returned successfully. In this way, a simple get request interface is completed.4. PHP handles post requests
In fact, it is similar to get requests, except that the method of getting the value is different, and the other operations are the same. $_GET['id'] becomes $_POST['id']Complete code:<?php $x = 5; $y = $_POST['id']; $z = $x + $y; echo "变量z为: $z"; ?>
5. Return json data
General interface requests return json data, so how does PHP return it? As follows:<?php $data = array('age' => 20, 'name' => '景天'); $response = array('code' => 200,'message' => '请求成功','data' => $data,); echo json_encode($response); ?>In this case, the returned content is:
{ "code":200, "message":"请求成功", "data":{ "age":20, "name":"景天" } }
Summary
Okay, so far, we have completed the first one interface. Although no specific business logic is involved, data can be returned normally. For more PHP related knowledge, please visitPHP Chinese website!
The above is the detailed content of How to write php application interface. For more information, please follow other related articles on the PHP Chinese website!