Repository navigation
Expand file tree
/
Copy pathexample.php
More file actions
65 lines (56 loc) · 1.98 KB
/
Copy pathexample.php
File metadata and controls
65 lines (56 loc) · 1.98 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
<?php
/**
* Perplexity Scraper — Scrapeless LLM Chat Scraper (PHP example)
*
* Docs: https://docs.scrapeless.com/en/llm-chat-scraper/quickstart/introduction/
* Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
*
* Run:
* export SCRAPELESS_API_TOKEN="your_api_token"
* php example.php
*/
$apiUrl = 'https://api.scrapeless.com/api/v2/scraper/execute';
$apiToken = getenv('SCRAPELESS_API_TOKEN') ?: 'YOUR_API_TOKEN';
$payload = [
'actor' => 'scraper.perplexity',
'input' => [
'prompt' => "Best supplements for better sleep",
'country' => "US",
],
// Optional: receive the result via webhook instead of the sync response.
// 'webhook' => ['url' => 'https://www.your-webhook.com'],
];
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
// A cold call takes roughly 15 seconds, so keep the timeout generous.
CURLOPT_TIMEOUT => 180,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'x-api-token: ' . $apiToken,
],
]);
$response = curl_exec($ch);
if ($response === false) {
fwrite(STDERR, 'cURL error: ' . curl_error($ch) . PHP_EOL);
exit(1);
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
fwrite(STDERR, "HTTP {$status}: {$response}" . PHP_EOL);
exit(1);
}
$data = json_decode($response, true);
$result = $data['task_result'] ?? [];
echo 'Status: ' . ($data['status'] ?? '') . PHP_EOL;
echo 'Task ID: ' . ($data['task_id'] ?? '') . PHP_EOL;
echo PHP_EOL . 'Answer:' . PHP_EOL . ($result['result_text'] ?? '') . PHP_EOL;
// Every list field is conditional — coalesce to [] before iterating.
$sources = $result['web_results'] ?? [];
echo PHP_EOL . 'Web results (' . count($sources) . '):' . PHP_EOL;
foreach ($sources as $item) {
echo ' - ' . ($item['name'] ?? '') . ' -> ' . ($item['url'] ?? '') . PHP_EOL;
}