-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeoNodeScraperAPI.php
More file actions
104 lines (87 loc) · 3.2 KB
/
Copy pathGeoNodeScraperAPI.php
File metadata and controls
104 lines (87 loc) · 3.2 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace App\Libraries;
use Exception;
class GeoNodeScraperAPI
{
private string $apiKey = 'MASUKKAN_API_KEY_ANDA_DI_SINI';
private string $apiUrl = 'https://scraper.geonode.io/v1/extract';
/**
* Scrape HTML from a target URL using GeoNode Scraper API.
*
* @param string $targetUrl
* @return string
* @throws Exception
*/
public function scrape(string $targetUrl): string
{
if (empty($this->apiKey) || $this->apiKey == 'MASUKKAN_API_KEY_ANDA_DI_SINI') {
throw new Exception('GeoNode Scraper API Key is not configured.');
}
$limitFile = WRITEPATH . 'geonode_limit.json';
$currentMonth = date('Y-m');
$count = 0;
// Check monthly request limit
if (file_exists($limitFile)) {
$data = json_decode(file_get_contents($limitFile), true);
if (isset($data['month']) && $data['month'] === $currentMonth) {
$count = (int)($data['count'] ?? 0);
}
}
if ($count >= 1499) {
throw new Exception('GeoNode Scraper API limit reached (max 1499 requests/month).');
}
$postData = [
'url' => $targetUrl,
'formats' => ['html'],
'render_js' => false,
'processing_mode' => 'sync',
'proxy' => [
'country' => 'US',
'type' => 'datacenter'
],
'headers' => [
'Accept-Language' => 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7'
]
];
$ch = curl_init($this->apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'x-api-key: ' . $this->apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_TIMEOUT, 35);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception('GeoNode cURL Error: ' . $error);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
$resData = json_decode($response, true);
$msg = $resData['message'] ?? ($resData['code'] ?? 'HTTP Code ' . $httpCode);
throw new Exception('GeoNode API Error: ' . $msg);
}
$result = json_decode($response, true);
$html = $result['data']['html'] ?? '';
if (empty($html)) {
throw new Exception('GeoNode API response did not contain HTML content.');
}
// Increment and save the monthly request count
$count++;
if (!is_dir(dirname($limitFile))) {
mkdir(dirname($limitFile), 0777, true);
}
file_put_contents($limitFile, json_encode([
'month' => $currentMonth,
'count' => $count
], JSON_PRETTY_PRINT));
return $html;
}
}