我已经在我的项目中实现了 swagger-php。请遵循以下建议的说明:
1) 下载 swagger-php(github.com/zircote/swagger-php) 和 swagger-ui(github.com/wordnik/swagger-ui)。将它们提取到您的工作区。
2) 在您的工作区中创建一个名为 swagger-api 的文件夹和名为 index.php 的空白 php 文件,然后粘贴以下代码。
<?php
use Swagger\Annotations as SWG;
/**
* @SWG\Resource(
* apiVersion="0.2",
* swaggerVersion="1.2",
* resourcePath="/api.php",
* basePath="http://localhost/swagger-api/"
* )
*/
// Run this in url
// localhost/index.php?action=get_app_list
// This is the API,show the list in array
/**
*
* @SWG\Api(
* path="/api.php?action=get_app_list",
* description="Operations about get app list",
* produces="['application/json']",
* @SWG\Operations(
* @SWG\Operation(
* method="GET",
* summary="Find facet by ID",
* notes="Returns a facet based on ID",
* type="ListResult",
* nickname="getAllResults",
* @SWG\ResponseMessages(
* @SWG\ResponseMessage(
* code=400,
* message="Invalid ID supplied"
* ),
* @SWG\ResponseMessage(
* code=404,
* message="facet not found"
* )
* )
* )
* )
* )
*/
function get_app_list()
{
//normally this info would be pulled from a database.
//build JSON array
$app_list = array(array("id" => 1, "name" => "Web Demo"),
array("id" => 2, "name" => "Audio Countdown"),
array("id" => 3, "name" => "The Tab Key"), array("id" => 4,
"name" => "Music Sleep Timer"));
return $app_list;
}
$possible_url = array("get_app_list");
$value = "An error has occurred";
if (isset($_GET["action"]) && in_array($_GET["action"], $possible_url))
{
switch ($_GET["action"])
{
case "get_app_list":
$value = get_app_list();
break;
$value = "Missing argument";
break;
}
}
//return JSON array
echo(json_encode($value));
?>
3) 在工作空间中创建一个名为 swagger-docs 的文件夹。
4) 打开您的终端并在您的工作区中找到 swagger-php 的位置(即 cd workpace/swagger-php)。
5)在你的终端执行以下命令 php swagger.phar /workspace/swagger-api -o /workspace/swagger-docs (这可以在我们包含 swagger.phar 文件的地方执行)。
6) 您将看到在您的 swagger docs 文件夹中创建的一些文件。
7) 在 swagger-ui/dist/ 中打开 index.html
8) 将 -: url: " http://petstore.swagger.wordnik.com/api/api-docs " 替换为 url:"http:localhost/swagger-docs"
9) 在浏览器中运行 localhost/swagger-ui/dist。