-
Notifications
You must be signed in to change notification settings - Fork 0
/
detik.php
29 lines (29 loc) · 1.01 KB
/
detik.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
<?php
require 'vendor/autoload.php';
$httpClient = new \GuzzleHttp\Client();
$response = $httpClient->get('https://www.detik.com/terpopuler');
$htmlString = (string) $response->getBody();
//add this line to suppress any warnings
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($htmlString);
$xpath = new DOMXPath($doc);
$titles = $xpath->evaluate('//article//h3/a');
$links = $xpath->evaluate('//article//h3/a/@href');
//$img = $xpath->evaluate('//article//span/img/@src');
foreach ($titles as $key => $title) {
$title = $title->textContent;
$url = $links[$key]->textContent;
//$img = $img[$key]->textContent;
//$output = array("title"=>$title->textContent, "links"=>$links[$key]->textContent);
$output[] = array(
'result' => array(
'title' => $title,
'url' => $url,
//'image' => $img
),
);
$ress = json_encode($output, JSON_PRETTY_PRINT).PHP_EOL;
}
echo $ress.PHP_EOL;
?>