-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-json.php
88 lines (63 loc) · 1.84 KB
/
09-json.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
$dataJson = "[5, 3, 4, 2, 1]";
$data = json_decode($dataJson);
echo implode(" - ", $data);
echo "<hr>";
$json = '{
"nama": "Nurul Huda",
"domisili": "Surabaya",
"usia": 23,
"wni": true,
"hobi": [
"Berenang", "Berlari", "Bertamasya"
]
}';
$mahasiswa = json_decode($json);
echo "Nama: {$mahasiswa->nama} <br>";
echo "Domisili: {$mahasiswa->domisili} <br>";
echo "Hobi: " . implode(", ", $mahasiswa->hobi);
echo "<hr>";
$mahasiswa = [
'nama' => 'Nurul Huda',
'domisili' => 'Surabaya'
];
echo json_encode($mahasiswa);
echo "<hr>";
$nilaiUjian = [70, 80, 50, 90];
echo json_encode($nilaiUjian);
echo "<hr>";
$listMahasiswaJSON = '[
{ "nama": "Nurul Huda" },
{ "nama": "Renza Ilhami Rizqi" },
{ "nama": "Taufan Aji" },
{ "nama": "Rahmad Dwi Oktanto" }
]';
$listMahasiswa = json_decode($listMahasiswaJSON);
foreach ($listMahasiswa as $key => $mahasiswa) {
echo "{$key}. Nama: {$mahasiswa->nama} <br>";
}
echo "<hr>";
$kataKunci = "corona";
$from = "2020-02-01";
$sortBy = "publishedAt";
$apiKey = "bea3d73524a94931af588526a5616c4c"; /* <-- Silakan register di newsapi.org untuk mendapatkan API_KEY */
$language = "en";
$alamatAPI = "http://newsapi.org/v2/everything?" .
"q={$kataKunci}&language={$language}&from={$from}" .
"&sortBy={$sortBy}&apiKey={$apiKey}";
echo $alamatAPI . "<br>";
# ambil data json dari $alamatAPI
$data = file_get_contents($alamatAPI);
# parsing variabel $data ke dalam array
$dataBerita = json_decode($data);
if ($dataBerita->status === "ok") {
# tampilkan menggunakan perulangan
foreach ($dataBerita->articles as $berita) {
echo "<h3><a href='{$berita->url}'>{$berita->title}</a></h3>";
if ($berita->urlToImage) {
echo "<img style='width: 10rem' src='{$berita->urlToImage}'>";
}
echo "<p>{$berita->description}</p>";
echo "<hr>";
}
}