API Screenshot en PHP : exemple de bout en bout
Script PHP 8+ avec cURL : création, attente active via /screenshot/info, téléchargement binaire, gestion d’erreurs stricte.
Quand PHP suffit
La majorité des hébergements activent déjà l’extension cURL. Vous pouvez donc intégrer ScreenshotCenter depuis un cron, un worker Symfony/Laravel, ou un simple script CLI sans ajouter de dépendance Composer. Le SDK PHP formalise les mêmes appels si vous préférez une API orientée objet.
Les noms de paramètres et les limites officielles sont recensés sur Developers.
Pipeline
| Étape | Point de terminaison | Résultat |
|---|---|---|
| Mettre en file | /api/v1/screenshot/create | JSON avec id numérique |
| Suivre l’état | /api/v1/screenshot/info | Même schéma jusqu’à finished |
| Enregistrer | /api/v1/screenshot/thumbnail | Octets PNG |
Script autonome
Les messages d’erreur et commentaires sont en français ; la structure correspond à l’article anglais.
true,
CURLOPT_TIMEOUT => 120,
]);
$corps = curl_exec($ch);
if ($corps === false) {
$err = curl_error($ch);
curl_close($ch);
throw new RuntimeException("Erreur cURL : $err");
}
$code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code !== 200) {
throw new RuntimeException("HTTP $code : $corps");
}
/** @var array $json */
$json = json_decode($corps, true, 512, JSON_THROW_ON_ERROR);
if (empty($json['success'])) {
throw new RuntimeException("Réponse API success=false : $corps");
}
if (!is_array($json['data'])) {
throw new RuntimeException('Champ data absent');
}
return $json['data'];
}
function telecharger(string $url, string $chemin): void {
$fp = fopen($chemin, 'wb');
if ($fp === false) {
throw new RuntimeException("Impossible d'écrire : $chemin");
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_FILE => $fp,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 120,
]);
$ok = curl_exec($ch);
$code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if ($ok === false || $code !== 200) {
@unlink($chemin);
throw new RuntimeException("Téléchargement échoué (HTTP $code)");
}
}
function creer_capture(string $base, string $cle, string $page): int {
$qs = http_build_query(['key' => $cle, 'url' => $page, 'country' => 'us']);
$d = http_get_json("$base/screenshot/create?$qs");
if (!isset($d['id'])) {
throw new RuntimeException('Identifiant manquant dans la réponse create');
}
return (int)$d['id'];
}
function info_capture(string $base, string $cle, int $id): array {
return http_get_json($base . '/screenshot/info?' . http_build_query(['key' => $cle, 'id' => $id]));
}
function attendre_fin(string $base, string $cle, int $id, int $pause = 2, int $max = 300): void {
$fin = time() + $max;
while (time() < $fin) {
$info = info_capture($base, $cle, $id);
$st = $info['status'] ?? '';
if ($st === 'finished') {
return;
}
if ($st === 'error') {
$msg = $info['error'] ?? 'erreur inconnue';
throw new RuntimeException("Échec capture : $msg");
}
sleep($pause);
}
throw new RuntimeException('Délai dépassé en attendant la capture');
}
$cle = env_obligatoire('SCREENSHOTCENTER_API_KEY');
$base = 'https://api.screenshotcenter.com/api/v1';
$cible = $argv[1] ?? 'https://example.com';
$id = creer_capture($base, $cle, $cible);
fwrite(STDOUT, "Capture mise en file, id=$id\n");
attendre_fin($base, $cle, $id);
$urlImg = $base . '/screenshot/thumbnail?' . http_build_query(['key' => $cle, 'id' => $id]);
$sortie = getcwd() . DIRECTORY_SEPARATOR . 'screenshot.png';
telecharger($urlImg, $sortie);
fwrite(STDOUT, "Fichier écrit : $sortie\n");
PHP-FPM et longues captures
Ne bloquez pas un pool FPM pendant plusieurs minutes : déléguez à un worker CLI ou à une file d’attente. Le script ci-dessus convient aux tâches planifiées et aux daemons.