So the kickoff on my 100 Days of Code journey didn’t go quite as planned. However, I think I am at a place where I can set myself up for success. Am I ready for this? Time will tell. Let’s talk about bookmarks.
Like many of you, I have a project list. A wish list of sorts. It seems to be a prerequisite for anyone who has written code as an enthusiast or professional to keep such a list. I really wonder how I came up with such a long list without making a dent in any of it. 100 Days of Code will help me make that dent!
First up – a basic REST API in PHP. I know. I know. PHP gets kicked and pushed around a lot these days. Remember this though – today’s web was built on the PHP stack. PERL, Java, and ASP where players too, but rightly or wrongly PHP held court. We’re talking the year 2000 here. Yes, I am that old. I digress. I wanted to create a basic REST API using PHP. I had an idea for a bookmark service. Dang you Google and your need to be everywhere!
I started my search on the you-tubes and ran across a familiar name – Brad Traversy. This is not the first time I have mentioned Brad. He is in my Chrome Extension With Angular 4 post a while back. I highly recommend dropping by his channel or his site. He had a video that met my criteria – PHP and REST API. Enough blabbering.
Bookmarks API Structure
Firstly, we need to make sure the solution is outlined. Here we are going to start with the directory and file structure needed to support the solution.
- api
- bookmarks
- read.php
- bookmarks
- config
- Database.php
- models
- Bookmarks.php
Bookmarks API Database
Secondly, there needs to be some sort of data for the API to interact with. Of course, I used MySQL for this. Actually, the whole solution is running on XAMPP. However, it should run on the PHP stack of your choosing. Here’s what that table looks like. I kept the hierarchy in the table. Quite a big no no, but this is a simple concept.
--
-- Table structure for table `bookmarks`
--
CREATE TABLE `bookmarks` (
`bookmark_id` int(11) NOT NULL,
`parent_id` int(11) UNSIGNED DEFAULT NULL,
`title` varchar(255) NOT NULL,
`url` text NOT NULL,
`create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `bookmarks`
--
INSERT INTO `bookmarks` (`bookmark_id`, `parent_id`, `title`, `url`, `create_date`) VALUES
(1, 3, 'Gurps Info', 'http://www.tdbd.net/gurps/', '2018-05-03 14:22:07'),
(2, 3, 'D20 Modern', 'https://www.limey.net/~magnum/DL/D20%20Modern/', '2018-05-03 14:22:07'),
(3, 0, 'D20 Games', '', '2018-05-03 17:26:00');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `bookmarks`
--
ALTER TABLE `bookmarks`
ADD PRIMARY KEY (`bookmark_id`);
Bookmarks API Code
I won’t bore you with all of the code since it will be on my github. Here is the heavy lifter read function.
public function read()
{
$query = 'SELECT
b.bookmark_id,
b.parent_id,
b.title,
b.url,
b.create_date
FROM
bookmarks b
left join bookmarks p on b.parent_id = p.bookmark_id
ORDER BY b.create_date DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
Bookmarks API Results
Once everything is hooked up, we can get results. http://bookmark/api/bookmarks/read.php
