Skip to content

Commit 9d75d41

Browse files
committed
Initial commit
1 parent 465df4a commit 9d75d41

11 files changed

+340
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# php-curl-crud-example
1+
# PHP cURL CRUD Example
2+
3+
Example code to accompany [this YouTube video](https://tobeadded/).
4+
5+
Note that the [init_curl.php](init_curl.php) file contains a placeholder for an API key. DO NOT check this into source code control containing a valid key. To keep this codebase *as simple as possible*, it doesn't use a separate .env file containing sensitive data like this. It is recommended however that you use such a file to store the API key, and add it to a file like .gitignore.

create.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
$ch = require "init_curl.php";
4+
5+
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/user/repos");
6+
//curl_setopt($ch, CURLOPT_POST, true);
7+
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
8+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($_POST));
9+
10+
$response = curl_exec($ch);
11+
12+
$status_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
13+
14+
curl_close($ch);
15+
16+
$data = json_decode($response, true);
17+
18+
if ($status_code === 422) {
19+
20+
echo "Invalid data: ";
21+
print_r($data["errors"]);
22+
exit;
23+
}
24+
25+
if ($status_code !== 201) {
26+
27+
echo "Unexpected status code: $status_code";
28+
var_dump($data);
29+
exit;
30+
}
31+
32+
?>
33+
<?php require "header.html" ?>
34+
35+
<h1>New Repository</h1>
36+
37+
<p>Repository created successfully.
38+
<a href="show.php?full_name=<?= $data["full_name"] ?>">Show</a>
39+
</p>
40+
41+
<?php require "footer.html" ?>
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+

delete.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
$ch = require "init_curl.php";
4+
5+
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/{$_POST['full_name']}");
6+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
7+
8+
$response = curl_exec($ch);
9+
10+
$status_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
11+
12+
curl_close($ch);
13+
14+
$data = json_decode($response, true);
15+
16+
if ($status_code !== 204) {
17+
18+
echo "Unexpected status code: $status_code";
19+
var_dump($data);
20+
exit;
21+
}
22+
23+
?>
24+
<?php require "header.html" ?>
25+
26+
<h1>Delete Repository</h1>
27+
28+
<p>Repository deleted successfully.
29+
<a href="index.php">Index</a>
30+
</p>
31+
32+
<?php require "footer.html" ?>
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+

edit.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
$full_name = $_GET["full_name"];
4+
5+
$ch = require "init_curl.php";
6+
7+
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/$full_name");
8+
9+
$response = curl_exec($ch);
10+
11+
curl_close($ch);
12+
13+
$data = json_decode($response, true);
14+
15+
?>
16+
<?php require "header.html" ?>
17+
18+
<h1>Edit Repository</h1>
19+
20+
<form method="post" action="update.php">
21+
22+
<input type="hidden" name="full_name" value="<?= $data["full_name"] ?>">
23+
24+
<label for="name">Name</label>
25+
<input type="text" name="name" id="name" value="<?= $data["name"] ?>">
26+
27+
<label for="description">Description</label>
28+
<textarea name="description" id="description"><?= htmlspecialchars($data["description"]) ?></textarea>
29+
30+
<button>Submit</button>
31+
</form>
32+
33+
<?php require "footer.html" ?>
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+

footer.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
</main>
2+
</body>
3+
</html>

header.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Example REST API Client</title>
6+
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.classless.min.css">
7+
</head>
8+
<body>
9+
10+
<main>

index.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
$ch = require "init_curl.php";
4+
5+
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/user/repos");
6+
7+
$response = curl_exec($ch);
8+
9+
curl_close($ch);
10+
11+
$data = json_decode($response, true);
12+
13+
?>
14+
<?php require "header.html" ?>
15+
16+
<h1>Repositories</h1>
17+
18+
<table>
19+
<thead>
20+
<tr>
21+
<th>Name</th>
22+
<th>Description</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
27+
<?php foreach ($data as $repository): ?>
28+
29+
<tr>
30+
<td>
31+
<a href="show.php?full_name=<?= $repository["full_name"] ?>">
32+
<?= $repository["name"] ?>
33+
</a>
34+
</td>
35+
<td><?= htmlspecialchars($repository["description"]) ?></td>
36+
</tr>
37+
38+
<?php endforeach; ?>
39+
40+
</tbody>
41+
</table>
42+
43+
<a href="new.php">New</a>
44+
45+
<?php require "footer.html" ?>
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+

init_curl.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* DO NOT CHECK THIS INTO SOURCE CODE CONTROL IF IT CONTAINS AN ACCESS TOKEN - SEE README FOR MORE DETAILS
5+
*/
6+
$headers = [
7+
"User-Agent: Example REST API Client",
8+
"Authorization: token YOUR_ACCESS_TOKEN"
9+
];
10+
11+
$ch = curl_init();
12+
13+
curl_setopt_array($ch, [
14+
CURLOPT_HTTPHEADER => $headers,
15+
CURLOPT_RETURNTRANSFER => true
16+
]);
17+
18+
return $ch;

new.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php require "header.html" ?>
2+
3+
<h1>New Repository</h1>
4+
5+
<form method="post" action="create.php">
6+
7+
<label for="name">Name</label>
8+
<input type="text" name="name" id="name">
9+
10+
<label for="description">Description</label>
11+
<textarea name="description" id="description"></textarea>
12+
13+
<button>Submit</button>
14+
</form>
15+
16+
<?php require "footer.html" ?>

show.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
$full_name = $_GET["full_name"];
4+
5+
$ch = require "init_curl.php";
6+
7+
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/$full_name");
8+
9+
$response = curl_exec($ch);
10+
11+
curl_close($ch);
12+
13+
$data = json_decode($response, true);
14+
15+
?>
16+
<?php require "header.html" ?>
17+
18+
<h1>Repository</h1>
19+
20+
<dl>
21+
<dt>Name</dt>
22+
<dd><?= $data["name"] ?></dd>
23+
<dt>Description</dt>
24+
<dd><?= htmlspecialchars($data["description"]) ?></dd>
25+
</dl>
26+
27+
<a href="edit.php?full_name=<?= $data["full_name"] ?>">Edit</a>
28+
29+
<form method="post" action="delete.php">
30+
<input type="hidden" name="full_name" value="<?= $data["full_name"] ?>">
31+
<button>Delete</button>
32+
</form>
33+
34+
<?php require "footer.html" ?>
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+

update.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
$ch = require "init_curl.php";
4+
5+
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/{$_POST['full_name']}");
6+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
7+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($_POST));
8+
9+
$response = curl_exec($ch);
10+
11+
$status_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
12+
13+
curl_close($ch);
14+
15+
$data = json_decode($response, true);
16+
17+
if ($status_code === 422) {
18+
19+
echo "Invalid data: ";
20+
print_r($data["errors"]);
21+
exit;
22+
}
23+
24+
if ($status_code !== 200) {
25+
26+
echo "Unexpected status code: $status_code";
27+
var_dump($data);
28+
exit;
29+
}
30+
31+
?>
32+
<?php require "header.html" ?>
33+
34+
<h1>Edit Repository</h1>
35+
36+
<p>Repository updated successfully.
37+
<a href="show.php?full_name=<?= $data["full_name"] ?>">Show</a>
38+
</p>
39+
40+
<?php require "footer.html" ?>
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+

0 commit comments

Comments
 (0)