Let there be bookmarks
This is a continuation of the bookmarks api I started with the 100 Days of Code challenge. Be sure to check it out.
Okay. That “let there be…” proclamation is probably a bit premature, however I can now add bookmarks through the API. The biggest take away from today – use valid json in the POST request. I spent way too long troubleshooting code that worked because of bad json. Here’s a snippet of the code I went over with a fine tooth comb:
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
if($data)
{
$bookmark->parent_id = $data['parent_id'];
$bookmark->title = $data['title'];
$bookmark->url = $data['url'];
// create bookmark
if($bookmark->create())
{
echo json_encode(
array('message' => 'Bookmark created.')
);
}
else
{
echo json_encode(
array('message' => 'Bookmark was not created.')
);
}
}
else
{
echo json_encode(
array('message' => 'Could not find bookmark data in POST.')
);
}
You’ll notice the little print_r($data) function. Yeah – It got that real. Turns out json_decode doesn’t work on non-valid json. Who knew? I kicked myself when I saw the error. I’ll share my rookie mistake so you do not have to suffer through it. Here are the malformed and the well-formed json bodies.
The Bad!
{
"parent_id" : "0",
"title" : "The Hypertext d20 SRD",
"url = "http://5e.d20srd.org/index.htm"
}
The Good!
{
"parent_id" : "0",
"title" : "The Hypertext d20 SRD",
"url" : "http://5e.d20srd.org/index.htm"
}

API Results
Once I got on the straight and narrow, I was able to create a new bookmark.

Below is the github log for yesterday and today. Swing by and check out my progress.
Day 2: June 12, 2019 Wednesday
Today’s Progress: Made a read single function to the bookmarks api.
Thoughts Continuing to push through.
Link(s) to work
Day 3: June 13, 2019 Thursday
Today’s Progress: Made a create function for the bookmarks api.
Thoughts Continuing to push through. I’ll probably add the delete today as well. Then spend some time thinking of auth/tokens.
Link(s) to work