-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
234 lines (199 loc) · 6.37 KB
/
Copy pathexample.php
File metadata and controls
234 lines (199 loc) · 6.37 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Scrappey - PHP Example
*
* This example demonstrates how to use the Scrappey API from PHP
* for web scraping with Cloudflare bypass and browser automation.
*
* Prerequisites:
* PHP 8.0+ with curl extension
*
* Run:
* php example.php
*
* Get your API key at: https://app.scrappey.com
*/
$API_KEY = getenv('SCRAPPEY_API_KEY') ?: 'YOUR_API_KEY';
$API_URL = 'https://publisher.scrappey.com/api/v1';
/**
* Send a request to the Scrappey API
*/
function scrappeyRequest(string $cmd, array $data = []): array {
global $API_KEY, $API_URL;
$payload = array_merge(['cmd' => $cmd], $data);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $API_URL . '?key=' . $API_KEY,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => 300,
]);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new Exception("cURL error: $error");
}
return json_decode($response, true) ?? throw new Exception("Failed to parse response");
}
/**
* Basic example: Simple GET request
*/
function basicExample(): void {
echo "\n=== Basic Example ===\n\n";
$result = scrappeyRequest('request.get', [
'url' => 'https://httpbin.org/get',
]);
if ($result['data'] === 'success') {
echo "Status: {$result['solution']['statusCode']}\n";
$preview = substr($result['solution']['response'], 0, 200) . '...';
echo "Response: $preview\n";
} else {
echo "Error: {$result['error']}\n";
}
}
/**
* Session example: Maintain cookies across requests
*/
function sessionExample(): void {
echo "\n=== Session Example ===\n\n";
// Create session
$sessionResult = scrappeyRequest('sessions.create', [
'proxyCountry' => 'UnitedStates',
'premiumProxy' => true,
]);
$sessionId = $sessionResult['session'];
echo "Created session: $sessionId\n";
try {
// Set cookie
scrappeyRequest('request.get', [
'url' => 'https://httpbin.org/cookies/set/test/value123',
'session' => $sessionId,
]);
// Verify cookie persists
$result = scrappeyRequest('request.get', [
'url' => 'https://httpbin.org/cookies',
'session' => $sessionId,
]);
echo "Cookies: {$result['solution']['response']}\n";
} finally {
// Destroy session
scrappeyRequest('sessions.destroy', [
'session' => $sessionId,
]);
echo "Destroyed session: $sessionId\n";
}
}
/**
* Browser actions example
*/
function browserActionsExample(): void {
echo "\n=== Browser Actions Example ===\n\n";
$result = scrappeyRequest('request.get', [
'url' => 'https://example.com',
'browserActions' => [
['type' => 'wait_for_selector', 'cssSelector' => 'body'],
['type' => 'execute_js', 'code' => 'document.title'],
['type' => 'scroll', 'cssSelector' => 'footer'],
],
]);
if ($result['data'] === 'success') {
echo "Page loaded, status: {$result['solution']['statusCode']}\n";
if (isset($result['solution']['javascriptReturn'])) {
echo "JS Return: " . json_encode($result['solution']['javascriptReturn']) . "\n";
}
} else {
echo "Error: {$result['error']}\n";
}
}
/**
* POST request example
*/
function postExample(): void {
echo "\n=== POST Example ===\n\n";
// Form data
$result = scrappeyRequest('request.post', [
'url' => 'https://httpbin.org/post',
'postData' => 'username=test&password=test123',
]);
echo "Form POST status: {$result['solution']['statusCode']}\n";
// JSON data
$result = scrappeyRequest('request.post', [
'url' => 'https://httpbin.org/post',
'postData' => json_encode(['key' => 'value', 'number' => 42]),
'customHeaders' => ['Content-Type' => 'application/json'],
]);
echo "JSON POST status: {$result['solution']['statusCode']}\n";
}
/**
* Cloudflare bypass example
*/
function cloudflareExample(): void {
echo "\n=== Cloudflare Bypass Example ===\n\n";
$result = scrappeyRequest('request.get', [
'url' => 'https://example-protected-site.com',
'cloudflareBypass' => true,
'premiumProxy' => true,
'proxyCountry' => 'UnitedStates',
]);
if ($result['data'] === 'success') {
echo "Successfully bypassed! Status: {$result['solution']['statusCode']}\n";
} else {
echo "Error: {$result['error']}\n";
}
}
/**
* Screenshot example
*/
function screenshotExample(): void {
echo "\n=== Screenshot Example ===\n\n";
$result = scrappeyRequest('request.get', [
'url' => 'https://example.com',
'screenshot' => true,
'screenshotWidth' => 1920,
'screenshotHeight' => 1080,
]);
if ($result['data'] === 'success' && !empty($result['solution']['screenshot'])) {
$imageData = base64_decode($result['solution']['screenshot']);
file_put_contents('screenshot.png', $imageData);
echo "Screenshot saved to screenshot.png\n";
} else {
echo "Error: " . ($result['error'] ?? 'No screenshot returned') . "\n";
}
}
/**
* Captcha solving example
*/
function captchaSolvingExample(): void {
echo "\n=== Captcha Solving Example ===\n\n";
$result = scrappeyRequest('request.get', [
'url' => 'https://example.com/protected',
'automaticallySolveCaptchas' => true,
'alwaysLoad' => ['recaptcha', 'hcaptcha', 'turnstile'],
]);
if ($result['data'] === 'success') {
echo "Captcha result: " . json_encode($result['solution']['captchaSolveResult'] ?? null) . "\n";
echo "Response length: " . strlen($result['solution']['response']) . "\n";
} else {
echo "Error: {$result['error']}\n";
}
}
// Main execution
echo "Scrappey PHP Examples\n";
echo str_repeat('=', 50) . "\n";
try {
basicExample();
sessionExample();
postExample();
// Uncomment to run additional examples:
// browserActionsExample();
// cloudflareExample();
// screenshotExample();
// captchaSolvingExample();
echo "\n✓ All examples completed!\n\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}