-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
115 lines (105 loc) · 4.12 KB
/
Copy pathexample.php
File metadata and controls
115 lines (105 loc) · 4.12 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
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Amazon Scraper — Scrapeless Scraping API (PHP example)
*
* Docs: https://apidocs.scrapeless.com/doc-857373
* Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
*
* The Amazon actor supports four scrape types selected via `input.type`:
* product | seller | keywords | rufus
*
* Run (requires the php-curl extension):
* export SCRAPELESS_API_TOKEN="your_api_token"
* php example.php # defaults to the "product" type
* php example.php keywords # or pass a type: product | seller | keywords | rufus
*/
$apiUrl = "https://api.scrapeless.com/api/v1/scraper/request";
$apiToken = getenv("SCRAPELESS_API_TOKEN") ?: "YOUR_API_TOKEN";
// Ready-to-use input payloads for each scrape type.
$sampleInputs = [
"product" => [
"type" => "product",
"url" => "https://www.amazon.com/dp/B0BQXHK363",
"zip_code" => "",
],
"seller" => [
"type" => "seller",
"url" => "https://www.amazon.com/sp?seller=A2XZ7JICGUQ1CX",
"zip_code" => "",
],
"keywords" => [
"type" => "keywords",
"keywords" => "Iphone+14+Pro+512GB",
"page" => "1",
"domain" => "com",
"zip_code" => "",
],
"rufus" => [
"type" => "rufus",
"keywords" => "macbook",
"domain" => "www.amazon.es",
"page" => "1",
],
];
$scrapeType = $argv[1] ?? "product";
if (!isset($sampleInputs[$scrapeType])) {
fwrite(STDERR, "Unknown type '{$scrapeType}'. Choose one of: product, seller, keywords, rufus\n");
exit(1);
}
$payload = ["actor" => "scraper.amazon", "input" => $sampleInputs[$scrapeType]];
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_TIMEOUT => 180,
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-token: " . $apiToken,
],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$response = curl_exec($ch);
if ($response === false) {
fwrite(STDERR, "Request failed: " . curl_error($ch) . PHP_EOL);
exit(1);
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// The Amazon actor distinguishes scenarios by HTTP status code.
switch ($status) {
case 200:
// Synchronous success: the body is the scraped data (shape depends on type).
$data = json_decode($response, true);
echo "[200] Success — '{$scrapeType}' data received." . PHP_EOL;
if ($scrapeType === "product") {
echo " ASIN: " . ($data["asin"] ?? "") . PHP_EOL;
echo " Brand: " . ($data["brand"] ?? "") . PHP_EOL;
echo " Price: " . ($data["final_price"] ?? "") . " (" . ($data["availability"] ?? "") . ")" . PHP_EOL;
} elseif ($scrapeType === "keywords") {
$organic = $data["result"]["organic"] ?? [];
echo " keyword: " . ($data["keyword"] ?? "") . " page: " . ($data["page"] ?? "") .
" — " . count($organic) . " organic results" . PHP_EOL;
foreach (array_slice($organic, 0, 3) as $item) {
echo " - " . ($item["title"] ?? "") . " :: " . ($item["price"] ?? "") . PHP_EOL;
}
}
echo PHP_EOL . " Raw response (truncated to 1500 chars):" . PHP_EOL;
echo " " . substr($response, 0, 1500) . PHP_EOL;
break;
case 201:
// Task accepted but still running. Retrieve it later by task id (see docs).
$body = json_decode($response, true);
echo "[201] Task in progress — message: " . ($body["message"] ?? "") .
", taskId: " . ($body["taskId"] ?? "") . PHP_EOL;
echo " Fetch the result later using the task id (see docs)." . PHP_EOL;
break;
case 400:
// Scraping failed — inspect the error code and message.
$body = json_decode($response, true);
echo "[400] Bad request — code: " . ($body["code"] ?? "") .
", message: " . ($body["message"] ?? "") . PHP_EOL;
break;
default:
echo "[{$status}] Unexpected response:" . PHP_EOL . $response . PHP_EOL;
exit(1);
}