简体   繁体   English

PHP中的Restful API?

[英]Restful API in Php?

I want to make restful API in php,i have a file 'index.php' in which php and html code is there,i am trying to make api for it. 我想在php中创建宁静的API,我有一个文件“ index.php”,其中有php和html代码,我正在尝试为此制作api。 So please suggest me is it right way to do? 所以请建议我这是正确的方法吗?

Here is my code: 这是我的代码:

<?php
$verb=$_SERVER['REQUEST_METHOD'];
if($verb == 'GET')
{
if(isset($_GET['filename'])){
$response = file_get_contents($_GET['filename']);
echo $response;
}
else{
die("ERROR: REQUIRED PARAMETERS NOT GIVEN!");
}
}
?>

First, you have to determine your API endpoints paths and verbs, like: 首先,您必须确定您的API端点路径和动词,例如:

GET /file/{filename} - will get a file
POST /file - will create a file

Second, you shouldn't use $_GET['filename'] , since your endpoint is for example GET /file/myfile.txt , where filename parameter is myfile.txt and this is a part of the path. 其次,您不应使用$_GET['filename'] ,因为端点是GET /file/myfile.txt ,其中filename参数是myfile.txt ,这是路径的一部分。 So you have to extract parameters from the path. 因此,您必须从路径中提取参数。

Third, it's better to use microframework for that purpose. 第三,为此目的最好使用微框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM