-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.php
41 lines (32 loc) · 998 Bytes
/
retrieve.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
// include the config file
require_once 'config.php';
//including the scraping file
include('scrape.php');
// connect to the database
require_once 'connect.php';
// query the database for the latest 30 posts
$sql = "SELECT * FROM posts ORDER BY published_at DESC LIMIT 30";
$result = mysqli_query($conn, $sql);
// create an empty array to store the posts
$posts = array();
// loop through the results and add each post to the array
while ($row = mysqli_fetch_assoc($result)) {
$post = array(
"id" => $row["id"],
"author" => $row["author"],
"title" => $row["title"],
"description" => $row["description"],
"url" => $row["url"],
"source" => $row["source"],
"image" => $row["image"],
"category" => $row["category"],
"published_at" => $row["published_at"]
);
array_push($posts, $post);
}
// output the posts array as JSON
header('Content-Type: application/json');
echo json_encode($posts);
// close the database connection
mysqli_close($conn);