How to write php application interface

angryTom
Release: 2023-02-27 11:20:02
Original
2672 people have browsed it

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: How to write php application interface

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[&#39;id&#39;];
$z = $x + $y;
echo "变量z为: $z"; 
?>
Copy after login

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)

How to write php application interface

How to write php application interface

##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[&#39;id&#39;];
$z = $x + $y;
echo "变量z为: $z"; 
?>
Copy after login

5. Return json data

General interface requests return json data, so how does PHP return it? As follows:

<?php    
$data = array(&#39;age&#39; => 20, &#39;name&#39; => &#39;景天&#39;);  
$response = array(&#39;code&#39;  => 200,&#39;message&#39; => &#39;请求成功&#39;,&#39;data&#39;  => $data,); 
echo json_encode($response);
?>
Copy after login

In this case, the returned content is:

{
    "code":200,
    "message":"请求成功",
    "data":{
        "age":20,
        "name":"景天"
    }
}
Copy after login

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 visit

PHP 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!