-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewsAPI.php
56 lines (51 loc) · 1.65 KB
/
NewsAPI.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
include "Fetch.php";
header("Content-Type: application/json");
class NewsAPI
{
private const API_KEY = "YOUR_API_KEY_HERE";
// Don't Change this
private const BASE_URL = "https://newsapi.org/v2/";
public function getEverything($keyword = null, $language = null, $searchIn = null) {
try {
if ($keyword === null) {
return 'keyword parameters are null';
}
$endpoint = "everything?";
if ($keyword !== null) {
$endpoint .= "q=${keyword}&";
}
if ($language !== null) {
$endpoint .= "language=${language}&";
}
if ($searchIn !== null) {
$endpoint .= "searchIn=${searchIn}&";
}
} catch (Exception $e) {
echo "Error" . $e;
} finally {
$fetch = new Fetch();
return $fetch->fetchData(self::BASE_URL . $endpoint . "apiKey=" . self::API_KEY);
}
}
public function getTopHeadlines($keyword = null, $country = null, $category = null) {
try {
$endpoint = "top-headlines?";
if ($keyword !== null) {
$endpoint .= "q=${keyword}&";
}
if ($country !== null) {
$endpoint .= "country=${country}&";
}
if ($category !== null) {
$endpoint .= "category=${category}&";
}
} catch (Exception $e) {
echo "Error" . $e;
} finally {
$fetch = new Fetch();
return $fetch->fetchData(self::BASE_URL . $endpoint . "apiKey=" . self::API_KEY);
}
}
}
?>