首页 > 解决方案 > 我如何格式化rest api

问题描述

我在本地主机上运行了一个非常基本的 RESTful api,它看起来像这样:

http://prntscr.com/nd8kpn

或文字:

{"course_id":"1",
"course_creator_id":"1",
"course_name":"Chanel Introduction",
"course_description":"In this course i will discuss info about myself and what you will be learning on my Chanel.",
"course_thumbnail":"\/public\/course_images\/0.jpg",
"date_added":"2019-04-17 15:25:39"
}
{"course_id":"2",
"course_creator_id":"1",
"course_name":"Getting started with Web Develop",
"course_description":"In this course, you will learn the basic concepts of Web Development. ",
"course_thumbnail":"\/public\/course_images\/1.jpg",
"date_added":"2019-04-17 15:25:39"
}
{"course_id":"3",
"course_creator_id":"1",
"course_name":"HTML and CSS Introduction",
"course_description":"In this course, I will go in depth on HTML, CSS and front end development of basic static webpages.",
"course_thumbnail":"\/public\/course_images\/2.jpg",
"date_added":"2019-04-17 15:25:39"
}
{"course_id":"4","course_creator_id":"1","course_name":"JavaScript and React Basics.","course_description":"In this course, we will dive deep into JavaScript and briefly go over the basics of React","course_thumbnail":"\/public\/course_images\/3.jpg","date_added":"2019-04-17 15:25:39"}
{"course_id":"5","course_creator_id":"1","course_name":"Building REST apis with Node and Express","course_description":"In this course, we will go in depth on REST apis and build one in the second half of the course.","course_thumbnail":"\/public\/course_images\/4.jpg","date_added":"2019-04-17 15:25:39"}
{"course_id":"6","course_creator_id":"1","course_name":"Building an e-books website from scratch","course_description":"In this course we will build an e-books website and combine everything from the previus 5 tutorials.","course_thumbnail":"\/public\/course_images\/5.jpg","date_added":"2019-04-17 15:25:39"}
{"course_id":"7","course_creator_id":"3","course_name":"What is Game Development","course_description":"In-depth look on game development (theory only, no code).","course_thumbnail":"\/public\/course_images\/6.jpg","date_added":"2019-04-17 15:25:39"}
{"course_id":"8","course_creator_id":"3","course_name":"C++ For Game Development","course_description":"In this massive course, i will teach you everything about C++ and touch on concepts for game development in C++.","course_thumbnail":"\/public\/course_images\/7.jpg","date_added":"2019-04-17 15:25:39"}
{"course_id":"9","course_creator_id":"3","course_name":"Introduction to Unreal Engine 4 ","course_description":"Here we will combine what you learnt in the previus course with Unreal Engine4 and start developing small games","course_thumbnail":"\/public\/course_images\/8.jpg","date_added":"2019-04-17 15:25:39"}
{"course_id":"10","course_creator_id":"3","course_name":"Making a turn based RPG with UE4 and C++","course_description":"In this tutorial, we will combined the previus 2 courses and make a turn based RPG from scratch","course_thumbnail":"\/public\/course_images\/9.jpg","date_added":"2019-04-17 15:25:39"}

我的代码如下:

include('../connect.php');
$stmt = $db->query('SELECT * FROM courses');
$result = $stmt->fetchAll(PDO::FETCH_OBJ);

foreach($result as $row){

    if(isset($_GET['course_id'])){
        if($row->course_id == $_GET['course_id']){
            echo json_encode($row);
        }
    } else if (isset($_GET['creator_id'])){
        if($row->course_creator_id == $_GET['creator_id']){
            echo json_encode($row);
        }
    } else {
        echo json_encode($row);
    }

}

正如我所说,非常简单。我将它用作测试 API,而不是作为生产 API。

我的问题是,为什么大多数 REST api 都非常像 facebook graph 的东西,但我的 REST API 总是有一个丑陋的格式?我如何使我的 REST api 漂亮?:D

标签: phprestpdo

解决方案


首先,您需要创建一个有效的 json 输出,然后您需要声明您的输出是一个 json,如下所示;

header('Content-Type: application/json');
include('../connect.php');
$stmt = $db->query('SELECT * FROM courses');
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$rows = [];
foreach($result as $row){
    if(isset($_GET['course_id'])){
        if($row->course_id == $_GET['course_id']){
            $rows[] = $row;
        }
    } else if (isset($_GET['creator_id'])){
        if($row->course_creator_id == $_GET['creator_id']){
             $rows[] = $row;
        }
    } else {
         $rows[] = $row;
    }
}
echo json_encode($rows, JSON_PRETTY_PRINT); 

JSON_PRETTY_PRINT 负责将数据打印成行而不是单行


推荐阅读