Skip to content

Commit 09ea17e

Browse files
authored
code
1 parent 49d5c4b commit 09ea17e

17 files changed

+887
-0
lines changed

Diff for: php_rest_myblog/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PHP REST API
2+
3+
> This is a simple PHP REST API from scratch with no framework.
4+
5+
## Quick Start
6+
7+
Import the myblog.sql file, change the params in the config/Database.php file to your own

Diff for: php_rest_myblog/ajaxCall.html

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
9+
<script src="ajaxCall.js"></script>
10+
11+
<style>
12+
body {
13+
text-align: center;
14+
font-family: "Helvetica", sans-serif;
15+
}
16+
h1 {
17+
font-size: 2em;
18+
font-weight: bold;
19+
}
20+
.box {
21+
border-radius: 5px;
22+
background-color: #eee;
23+
padding: 20px 5px;
24+
}
25+
button {
26+
color: white;
27+
background-color: #4791d0;
28+
border-radius: 5px;
29+
border: 1px solid #4791d0;
30+
padding: 5px 10px 8px 10px;
31+
}
32+
button:hover {
33+
background-color: #0F5897;
34+
border: 1px solid #0F5897;
35+
}
36+
</style>
37+
</head>
38+
<body>
39+
40+
<div style="float:left; width:50%;">
41+
<h1>Data Finder</h1>
42+
<p class="message box"> The message will go here </p>
43+
<p><button id="getMessage"> Get Message </button></p>
44+
</div>
45+
46+
47+
<div style="float:right; width:50%;">
48+
<h1>Data Sender</h1>
49+
<form id="apiform">
50+
51+
<input type="text" name="title" placeholder="title" autocomplete="on" ><br>
52+
<textarea name="body" id="" cols="30" rows="10" placeholder="body" autocomplete="on" ></textarea><br>
53+
<input type="text" name="author" placeholder="author" autocomplete="on" ><br>
54+
<input type="text" name="category_id" placeholder="category_id" autocomplete="on" ><br><br>
55+
56+
<button id="postMessage"> Post Message </button>
57+
</form>
58+
</div>
59+
60+
61+
</body>
62+
</html>
63+
64+
65+
66+
67+
68+

Diff for: php_rest_myblog/ajaxCall.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
//POST REQUEST
3+
4+
$(document).ready(function(){
5+
$('#postMessage').click(function(e){
6+
e.preventDefault();
7+
8+
//serialize form data
9+
var url = $('form').serialize();
10+
11+
//function to turn url to an object
12+
function getUrlVars(url) {
13+
var hash;
14+
var myJson = {};
15+
var hashes = url.slice(url.indexOf('?') + 1).split('&');
16+
for (var i = 0; i < hashes.length; i++) {
17+
hash = hashes[i].split('=');
18+
myJson[hash[0]] = hash[1];
19+
}
20+
return JSON.stringify(myJson);
21+
}
22+
23+
//pass serialized data to function
24+
var test = getUrlVars(url);
25+
26+
//post with ajax
27+
$.ajax({
28+
type:"POST",
29+
url: "/Work folders/OOP php/RESTFUL traversy/php_rest_myblog/api/post/create.php",
30+
data: test,
31+
ContentType:"application/json",
32+
33+
success:function(){
34+
alert('successfully posted');
35+
},
36+
error:function(){
37+
alert('Could not be posted');
38+
}
39+
40+
});
41+
});
42+
});
43+
44+
45+
//GET REQUEST
46+
47+
document.addEventListener('DOMContentLoaded',function(){
48+
document.getElementById('getMessage').onclick=function(){
49+
50+
var req;
51+
req=new XMLHttpRequest();
52+
req.open("GET", '/Work folders/OOP php/RESTFUL traversy/php_rest_myblog/api/post/read.php',true);
53+
req.send();
54+
55+
req.onload=function(){
56+
var json=JSON.parse(req.responseText);
57+
58+
//limit data called
59+
var son = json.filter(function(val) {
60+
return (val.id >= 4);
61+
});
62+
63+
var html = "";
64+
65+
//loop and display data
66+
son.forEach(function(val) {
67+
var keys = Object.keys(val);
68+
69+
html += "<div class = 'cat'>";
70+
keys.forEach(function(key) {
71+
html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
72+
});
73+
html += "</div><br>";
74+
});
75+
76+
//append in message class
77+
document.getElementsByClassName('message')[0].innerHTML=html;
78+
};
79+
};
80+
});
81+

Diff for: php_rest_myblog/api/category/create.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: POST');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Category.php';
10+
// Instantiate DB & connect
11+
$database = new Database();
12+
$db = $database->connect();
13+
14+
// Instantiate blog post object
15+
$category = new Category($db);
16+
17+
// Get raw posted data
18+
$data = json_decode(file_get_contents("php://input"));
19+
20+
$category->name = $data->name;
21+
22+
// Create Category
23+
if($category->create()) {
24+
echo json_encode(
25+
array('message' => 'Category Created')
26+
);
27+
} else {
28+
echo json_encode(
29+
array('message' => 'Category Not Created')
30+
);
31+
}

Diff for: php_rest_myblog/api/category/delete.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: DELETE');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Category.php';
10+
// Instantiate DB & connect
11+
$database = new Database();
12+
$db = $database->connect();
13+
14+
// Instantiate blog post object
15+
$category = new Category($db);
16+
17+
// Get raw posted data
18+
$data = json_decode(file_get_contents("php://input"));
19+
20+
// Set ID to UPDATE
21+
$category->id = $data->id;
22+
23+
// Delete post
24+
if($category->delete()) {
25+
echo json_encode(
26+
array('message' => 'Category deleted')
27+
);
28+
} else {
29+
echo json_encode(
30+
array('message' => 'Category not deleted')
31+
);
32+
}

Diff for: php_rest_myblog/api/category/read.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
6+
include_once '../../config/Database.php';
7+
include_once '../../models/Category.php';
8+
9+
// Instantiate DB & connect
10+
$database = new Database();
11+
$db = $database->connect();
12+
13+
// Instantiate category object
14+
$category = new Category($db);
15+
16+
// Category read query
17+
$result = $category->read();
18+
19+
// Get row count
20+
$num = $result->rowCount();
21+
22+
// Check if any categories
23+
if($num > 0) {
24+
// Cat array
25+
$cat_arr = array();
26+
$cat_arr['data'] = array();
27+
28+
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
29+
extract($row);
30+
31+
$cat_item = array(
32+
'id' => $id,
33+
'name' => $name
34+
);
35+
36+
// Push to "data"
37+
array_push($cat_arr['data'], $cat_item);
38+
}
39+
40+
// Turn to JSON & output
41+
echo json_encode($cat_arr);
42+
43+
} else {
44+
// No Categories
45+
echo json_encode(
46+
array('message' => 'No Categories Found')
47+
);
48+
}

Diff for: php_rest_myblog/api/category/read_single.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
// Headers
4+
header('Access-Control-Allow-Origin: *');
5+
header('Content-Type: application/json');
6+
7+
include_once '../../config/Database.php';
8+
include_once '../../models/Category.php';
9+
// Instantiate DB & connect
10+
$database = new Database();
11+
$db = $database->connect();
12+
// Instantiate blog category object
13+
$category = new Category($db);
14+
15+
// Get ID
16+
$category->id = isset($_GET['id']) ? $_GET['id'] : die();
17+
18+
// Get post
19+
$category->read_single();
20+
21+
// Create array
22+
$category_arr = array(
23+
'id' => $category->id,
24+
'name' => $category->name
25+
);
26+
27+
// Make JSON
28+
print_r(json_encode($category_arr));

Diff for: php_rest_myblog/api/category/update.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: PUT');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Category.php';
10+
// Instantiate DB & connect
11+
$database = new Database();
12+
$db = $database->connect();
13+
14+
// Instantiate blog post object
15+
$category = new Category($db);
16+
17+
// Get raw posted data
18+
$data = json_decode(file_get_contents("php://input"));
19+
20+
// Set ID to UPDATE
21+
$category->id = $data->id;
22+
23+
$category->name = $data->name;
24+
25+
// Update post
26+
if($category->update()) {
27+
echo json_encode(
28+
array('message' => 'Category Updated')
29+
);
30+
} else {
31+
echo json_encode(
32+
array('message' => 'Category not updated')
33+
);
34+
}

Diff for: php_rest_myblog/api/post/create.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
// Headers
3+
header('Access-Control-Allow-Origin: *');
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Methods: POST');
6+
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
7+
8+
include_once '../../config/Database.php';
9+
include_once '../../models/Post.php';
10+
11+
// Instantiate DB & connect
12+
$database = new Database();
13+
$db = $database->connect();
14+
15+
// Instantiate blog post object
16+
$post = new Post($db);
17+
18+
// Get raw posted data
19+
$data = json_decode(file_get_contents("php://input"));
20+
21+
$post->title = $data->title;
22+
$post->body = $data->body;
23+
$post->author = $data->author;
24+
$post->category_id = $data->category_id;
25+
26+
// Create post
27+
if($post->create()) {
28+
echo json_encode(
29+
array('message' => 'Post Created')
30+
);
31+
} else {
32+
echo json_encode(
33+
array('message' => 'Post Not Created')
34+
);
35+
}
36+

0 commit comments

Comments
 (0)