-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-scrapingbee.php
More file actions
161 lines (132 loc) · 4.75 KB
/
Copy pathtest-scrapingbee.php
File metadata and controls
161 lines (132 loc) · 4.75 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
<?php
/**
* Script de test pour ScrapingBee
* Usage: php test-scrapingbee.php
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "=== Test ScrapingBee ===\n\n";
// Charger la configuration
$configFile = __DIR__ . '/scraping-config.json';
if (!file_exists($configFile)) {
die("ERREUR: scraping-config.json non trouvé\n");
}
$config = json_decode(file_get_contents($configFile), true);
if (!$config) {
die("ERREUR: scraping-config.json invalide (JSON mal formé)\n");
}
echo "1. Configuration chargée\n";
echo " Service: " . ($config['service'] ?: '(vide)') . "\n";
echo " API Key: " . (empty($config['api_key']) ? '(vide)' : substr($config['api_key'], 0, 10) . '...') . "\n\n";
if (empty($config['service'])) {
die("ERREUR: 'service' non configuré dans scraping-config.json\n");
}
if (empty($config['api_key'])) {
die("ERREUR: 'api_key' non configuré dans scraping-config.json\n");
}
// URL à tester
$testUrl = 'https://www.seo.com/fr/tools/ai/';
echo "2. Test de récupération de: $testUrl\n\n";
// Construire l'URL ScrapingBee
$params = [
'api_key' => $config['api_key'],
'url' => $testUrl,
'render_js' => 'true',
'premium_proxy' => 'true',
'country_code' => 'fr',
'block_ads' => 'true',
'wait' => '5000',
];
$apiUrl = 'https://app.scrapingbee.com/api/v1/?' . http_build_query($params);
echo "3. Appel API ScrapingBee...\n";
echo " URL API (masquée): https://app.scrapingbee.com/api/v1/?api_key=***&url=" . urlencode($testUrl) . "&...\n\n";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => false,
]);
$startTime = microtime(true);
$html = curl_exec($ch);
$duration = round(microtime(true) - $startTime, 2);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo "4. Résultats:\n";
echo " Durée: {$duration}s\n";
echo " Code HTTP: $httpCode\n";
echo " Taille réponse: " . strlen($html) . " caractères\n";
if ($error) {
echo " ERREUR cURL: $error\n";
}
echo "\n";
// Analyser la réponse
if ($httpCode === 200) {
echo "5. Succès HTTP 200!\n";
// Vérifier si c'est du HTML valide
if (strpos($html, '<html') !== false || strpos($html, '<!DOCTYPE') !== false) {
echo " HTML valide détecté\n";
// Vérifier si c'est une page Cloudflare
$cloudflarePatterns = [
'Checking your browser',
'Just a moment...',
'cf-browser-verification',
'challenge-platform',
'_cf_chl_opt',
];
$isCloudflare = false;
foreach ($cloudflarePatterns as $pattern) {
if (stripos($html, $pattern) !== false) {
$isCloudflare = true;
echo " ATTENTION: Page Cloudflare détectée (pattern: $pattern)\n";
break;
}
}
if (!$isCloudflare) {
echo " Pas de protection Cloudflare détectée\n";
// Afficher un extrait
$title = '';
if (preg_match('/<title[^>]*>(.*?)<\/title>/is', $html, $matches)) {
$title = trim(strip_tags($matches[1]));
}
echo " Titre de la page: " . ($title ?: '(non trouvé)') . "\n";
// Chercher des éléments clés
$hasJsonLd = strpos($html, 'application/ld+json') !== false;
echo " JSON-LD présent: " . ($hasJsonLd ? 'Oui' : 'Non') . "\n";
}
} else {
echo " ATTENTION: La réponse ne semble pas être du HTML\n";
echo " Premiers 500 caractères:\n";
echo " " . substr($html, 0, 500) . "\n";
}
} else {
echo "5. ÉCHEC - Code HTTP: $httpCode\n";
// Décoder les erreurs ScrapingBee
$errorData = json_decode($html, true);
if ($errorData) {
echo " Message d'erreur: " . ($errorData['message'] ?? 'Inconnu') . "\n";
if (isset($errorData['error'])) {
echo " Détails: " . $errorData['error'] . "\n";
}
} else {
echo " Réponse brute: " . substr($html, 0, 300) . "\n";
}
// Codes d'erreur ScrapingBee courants
$errorCodes = [
401 => "Clé API invalide ou expirée",
402 => "Crédits épuisés",
403 => "Accès refusé (vérifiez votre plan)",
429 => "Rate limit atteint",
500 => "Erreur serveur ScrapingBee",
502 => "Erreur proxy ScrapingBee",
503 => "Service indisponible",
];
if (isset($errorCodes[$httpCode])) {
echo " Explication: " . $errorCodes[$httpCode] . "\n";
}
}
echo "\n=== Fin du test ===\n";