Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.geekflare.com/llms.txt

Use this file to discover all available pages before exploring further.

No official PHP SDK exists yet, but the Geekflare REST API is straightforward to call from PHP using either native cURL or the popular Guzzle HTTP client.

Prerequisites

Installation

No SDK is needed for cURL. To use Guzzle:
composer require guzzlehttp/guzzle vlucas/phpdotenv

Set your API key

.env
GEEKFLARE_API_KEY=your_api_key_here
Load it with phpdotenv:
require_once __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$apiKey = $_ENV['GEEKFLARE_API_KEY'];
Never hardcode your API key. Add .env to .gitignore.

Web Scraping

<?php
require_once __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$apiKey = $_ENV['GEEKFLARE_API_KEY'];

$payload = json_encode([
    'url'       => 'https://toscrape.com/',
    'renderJS'  => true,
    'blockAds'  => true,
    'format'    => 'html,markdown',
    'device'    => 'desktop',
]);

$ch = curl_init('https://api.geekflare.com/webscraping');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $payload,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'x-api-key: ' . $apiKey,
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200) {
    $error = json_decode($response, true);
    throw new RuntimeException("API error [{$error['apiCode']}]: {$error['message']}");
}

$result = json_decode($response, true);
print_r($result['data']);
Key parameters:
ParameterTypeDescription
urlstringRequired. The URL to scrape
renderJSboolWait for JavaScript to execute
blockAdsboolBlock ads before scraping
formatstringComma-separated: html, markdown
devicestring"desktop" or "mobile"
proxyCountrystringRoute via country code, e.g. "us"
stealthboolEnable stealth mode
waitTimefloatExtra wait in seconds

<?php
require_once __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$payload = json_encode([
    'query'    => 'best PHP frameworks 2024',
    'limit'    => 10,
    'location' => 'us',
    'source'   => 'web',
    'time'     => 'm',
    'format'   => 'json',
]);

$ch = curl_init('https://api.geekflare.com/search');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $payload,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'x-api-key: ' . $_ENV['GEEKFLARE_API_KEY'],
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$result = json_decode($response, true);

foreach ($result['data'] as $item) {
    echo $item['title'] . ' — ' . $item['url'] . PHP_EOL;
    echo $item['snippet'] . PHP_EOL . PHP_EOL;
}

Screenshot

<?php
require_once __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$payload = json_encode([
    'url'            => 'https://example.com',
    'type'           => 'png',
    'fullPage'       => true,
    'device'         => 'desktop',
    'viewportWidth'  => 1280,
    'viewportHeight' => 800,
    'blockAds'       => true,
    'hideCookie'     => true,
    'quality'        => 90,
]);

$ch = curl_init('https://api.geekflare.com/screenshot');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $payload,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'x-api-key: ' . $_ENV['GEEKFLARE_API_KEY'],
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$result = json_decode($response, true);
// $result['data'] is the screenshot URL directly
echo "Screenshot URL: " . $result['data'] . PHP_EOL;

Error handling

<?php
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (curl_errno($ch)) {
    throw new RuntimeException('cURL error: ' . curl_error($ch));
}

$result = json_decode($response, true);

if ($httpCode !== 200 || $result['apiStatus'] !== 'success') {
    $code    = $result['apiCode'] ?? $httpCode;
    $message = $result['message'] ?? 'Unknown error';

    match ($code) {
        401 => throw new RuntimeException("Invalid or missing API key."),
        402 => throw new RuntimeException("API credits exhausted."),
        403 => throw new RuntimeException("Endpoint not available on your plan."),
        429 => throw new RuntimeException("Rate limit exceeded. Back off and retry."),
        default => throw new RuntimeException("API error [{$code}]: {$message}"),
    };
}

Common error codes

CodeMeaning
401Missing or invalid x-api-key
402API credits exhausted
403Endpoint requires a higher plan
404Wrong endpoint URL or HTTP method
429Rate limit exceeded

Next steps