Scraper avec PHP

Avec Guzzle, le client HTTP standard de l'écosystème PHP, un appel WyndPath tient en quelques lignes.

Installation

composer require guzzlehttp/guzzle

Premier appel

use GuzzleHttp\Client;

$client = new Client();
$resp = $client->get("https://api.wyndpath.com/v1/", [
    "query" => [
        "api_key" => "VOTRE_CLE",
        "url"     => "https://exemple.com",
    ],
]);
echo $resp->getStatusCode(), "\n";
echo $resp->getBody();

Rendu JavaScript et pays

$resp = $client->get("https://api.wyndpath.com/v1/", [
    "query" => [
        "api_key"   => "VOTRE_CLE",
        "url"       => "https://exemple.com",
        "render_js" => 1,
        "country"   => "fr",
        "max_cost"  => 15,
    ],
]);

Lire les crédits

echo $resp->getHeaderLine("X-WyndPath-Credits");

Requête POST

$resp = $client->post("https://api.wyndpath.com/v1/", [
    "query"   => ["api_key" => "VOTRE_CLE", "url" => "https://exemple.com/api"],
    "headers" => ["Content-Type" => "application/json"],
    "body"    => json_encode(["q" => "pikachu"]),
]);

Gérer les erreurs

use GuzzleHttp\Exception\RequestException;

try {
    $resp = $client->get("https://api.wyndpath.com/v1/", [
        "query" => ["api_key" => "VOTRE_CLE", "url" => "https://exemple.com"],
    ]);
    $html = (string) $resp->getBody();
} catch (RequestException $e) {
    $r = $e->getResponse();
    echo "Erreur ", $r->getStatusCode(), " : ", $r->getBody();
}
✓ Payé au succès. Un appel qui échoue ne consomme aucun crédit.