Skip to content

Commit 41d2ff8

Browse files
added fetch meta api
1 parent 39d703c commit 41d2ff8

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dist/
44
info.txt
55
.env
66
node_modules/
7-
vendor/
7+
vendor/
8+
/$__*/

api/fetchmeta.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
4+
use DiDom\Document;
5+
6+
function fetchMeta(string $url, bool $min = true): array
7+
{
8+
$document = new Document($url, true);
9+
$metadata = $document->find("meta");
10+
11+
$data = [];
12+
foreach ($metadata as $element) {
13+
$name = $element->getAttribute("name");
14+
$property = $element->getAttribute("property");
15+
$content = $element->getAttribute("content");
16+
17+
// Add the extracted metadata to the data array
18+
if ($min) {
19+
$key = ($name) ? $name : $property;
20+
if ($key) {
21+
$data[$key] = $content;
22+
}
23+
} else {
24+
$data[] = [
25+
"name" => $name,
26+
"property" => $property,
27+
"content" => $content,
28+
];
29+
}
30+
}
31+
32+
return $data;
33+
}
34+
35+
36+
// Get Values from the POST request
37+
$url = $_POST['url'];
38+
$min = isset($_POST['min']) ? true : false;
39+
40+
// Validate the URL
41+
if (empty($url)) {
42+
http_response_code(400); // Bad Request
43+
echo json_encode(['error' => 'URL parameter is missing.']);
44+
exit;
45+
}
46+
47+
$data = fetchMeta($url, $min);
48+
echo json_encode($data, JSON_PRETTY_PRINT);

api/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
$data = ['error' => 'API yet to be created!'];
3+
$data = ['error' => 'No API @ current route!'];
44
echo json_encode($data, JSON_PRETTY_PRINT);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"require": {
3-
"vlucas/phpdotenv": "^5.5"
3+
"vlucas/phpdotenv": "^5.5",
4+
"imangazaliev/didom": "^2.0"
45
}
56
}

composer.lock

Lines changed: 53 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/api.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
'/' => ['index.php', 'root']
1616
];
1717

18-
$API_POST_ROUTES = [];
18+
$API_POST_ROUTES = [
19+
'/fetch/meta' => ['fetchmeta.php', 'fetch.meta']
20+
];
1921

2022

2123
define('API_ROUTES', [

0 commit comments

Comments
 (0)