PHP code example of spidra / spidra-php

1. Go to this page and download the library: Download spidra/spidra-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spidra / spidra-php example snippets


use Spidra\SpidraClient;

$spidra = new SpidraClient('spd_YOUR_API_KEY');

$job = $spidra->scrape->run([
    'urls'   => [['url' => 'https://news.ycombinator.com']],
    'prompt' => 'List the top 5 stories with title, points, and comment count',
    'output' => 'json',
]);

print_r($job['result']['content']);

$job = $spidra->scrape->run([
    'urls'   => [['url' => 'https://example.com/pricing']],
    'prompt' => 'Extract all pricing plans with name, price, and 

$job = $spidra->scrape->run([
    'urls'   => [['url' => 'https://jobs.example.com/senior-engineer']],
    'prompt' => 'Extract the job listing details',
    'output' => 'json',
    'schema' => [
        'type'       => 'object',
        'null']],
            'salary_max' => ['type' => ['number', 'null']],
            'skills'     => ['type' => 'array', 'items' => ['type' => 'string']],
        ],
    ],
]);

$job = $spidra->scrape->run([
    'urls'         => [['url' => 'https://www.amazon.de/gp/bestsellers']],
    'prompt'       => 'List the top 10 products with name and price',
    'useProxy'     => true,
    'proxyCountry' => 'de',
]);

$job = $spidra->scrape->run([
    'urls'    => [['url' => 'https://app.example.com/dashboard']],
    'prompt'  => 'Extract the monthly revenue and active user count',
    'cookies' => 'session=abc123; auth_token=xyz789',
]);

$job = $spidra->scrape->run([
    'urls' => [
        [
            'url'     => 'https://example.com/products',
            'actions' => [
                ['type' => 'click', 'selector' => '#accept-cookies'],
                ['type' => 'wait',  'duration' => 1000],
                ['type' => 'scroll', 'to' => '80%'],
            ],
        ],
    ],
    'prompt' => 'Extract all product names and prices',
]);

// CSS selector
['type' => 'click', 'selector' => "button[data-testid='submit']"]

// Plain English — AI finds the element
['type' => 'click', 'value' => 'Accept all cookies button']

// Type into a field
['type' => 'type', 'selector' => "input[name='q']", 'value' => 'wireless headphones']

// Wait for content to load
['type' => 'wait', 'duration' => 2000]

// Scroll to bottom
['type' => 'scroll', 'to' => '100%']

$job = $spidra->scrape->run([
    'urls' => [
        [
            'url'     => 'https://books.toscrape.com/catalogue/category/books/mystery_3/index.html',
            'actions' => [
                [
                    'type'            => 'forEach',
                    'observe'         => 'Find all book cards in the product grid',
                    'mode'            => 'inline',
                    'captureSelector' => 'article.product_pod',
                    'maxItems'        => 20,
                    'itemPrompt'      => 'Extract title, price, and star rating. Return as JSON: {title, price, star_rating}',
                ],
            ],
        ],
    ],
    'prompt' => 'Return a clean JSON array of all books',
    'output' => 'json',
]);

[
    'type'            => 'forEach',
    'observe'         => 'Find all book title links in the product grid',
    'mode'            => 'navigate',
    'captureSelector' => 'article.product_page',
    'maxItems'        => 10,
    'waitAfterClick'  => 800,
    'itemPrompt'      => 'Extract title, price, star rating, and availability. Return as JSON.',
]

[
    'type'            => 'forEach',
    'observe'         => 'Find all room type cards',
    'mode'            => 'click',
    'captureSelector' => "[role='dialog']",
    'maxItems'        => 8,
    'waitAfterClick'  => 1200,
    'itemPrompt'      => 'Extract room name, bed type, price per night, and amenities. Return as JSON.',
]

[
    'type'       => 'forEach',
    'observe'    => 'Find all book title links',
    'mode'       => 'navigate',
    'maxItems'   => 40,
    'pagination' => [
        'nextSelector' => 'li.next > a',
        'maxPages'     => 3, // 3 additional pages beyond the first
    ],
]

[
    'type'            => 'forEach',
    'observe'         => 'Find all book title links',
    'mode'            => 'navigate',
    'captureSelector' => 'article.product_page',
    'maxItems'        => 5,
    'waitAfterClick'  => 1000,
    'actions'         => [
        ['type' => 'scroll', 'to' => '50%'],
    ],
    'itemPrompt' => 'Extract title, price, and full description. Return as JSON.',
]

// Submit a job and get the jobId immediately
$queued = $spidra->scrape->submit([
    'urls'   => [['url' => 'https://example.com/listings']],
    'prompt' => 'Extract all property listings',
    'output' => 'json',
]);

$jobId = $queued['jobId'];

// Check status at any point
$result = $spidra->scrape->get($jobId);

if ($result['status'] === 'completed') {
    print_r($result['result']['content']);
} elseif ($result['status'] === 'failed') {
    echo $result['error'];
}

$job = $spidra->scrape->run(
    params:       [...],
    timeout:      180, // wait up to 3 minutes
    pollInterval: 5,   // check every 5 seconds
);

$batch = $spidra->batch->run([
    'urls'     => [
        'https://shop.example.com/product/1',
        'https://shop.example.com/product/2',
        'https://shop.example.com/product/3',
    ],
    'prompt'   => 'Extract product name, price, and availability',
    'output'   => 'json',
    'useProxy' => true,
]);

foreach ($batch['items'] as $item) {
    if ($item['status'] === 'completed') {
        echo $item['url'] . ': ';
        print_r($item['result']);
    } elseif ($item['status'] === 'failed') {
        echo $item['url'] . ' failed: ' . $item['error'] . "\n";
    }
}

$queued = $spidra->batch->submit([
    'urls'   => ['https://example.com/1', 'https://example.com/2'],
    'prompt' => 'Extract the page title',
]);

$batchId = $queued['batchId'];

// Later, after checking status
$result = $spidra->batch->get($batchId);
if ($result['failedCount'] > 0) {
    $spidra->batch->retry($batchId);
}

$result = $spidra->batch->cancel($batchId);
echo "Cancelled {$result['cancelledItems']} items, refunded {$result['creditsRefunded']} credits\n";

$result = $spidra->batch->list(page: 1, limit: 20);

foreach ($result['jobs'] as $job) {
    echo "{$job['uuid']} {$job['status']} {$job['completedCount']}/{$job['totalUrls']}\n";
}

$job = $spidra->crawl->run([
    'baseUrl'              => 'https://competitor.com/blog',
    'crawlInstruction'     => 'Find all blog posts published in 2024',
    'transformInstruction' => 'Extract the title, author, publish date, and a one-sentence summary',
    'maxPages'             => 30,
    'useProxy'             => true,
]);

foreach ($job['result'] as $page) {
    echo $page['url'] . "\n";
    print_r($page['data']);
}

$queued = $spidra->crawl->submit([
    'baseUrl'              => 'https://example.com/docs',
    'crawlInstruction'     => 'Find all documentation pages',
    'transformInstruction' => 'Extract the page title and main content summary',
    'maxPages'             => 50,
]);

$jobId = $queued['jobId'];

// Check status later
$status = $spidra->crawl->get($jobId);

$result = $spidra->crawl->pages($jobId);

foreach ($result['pages'] as $page) {
    echo $page['url'] . ' — ' . $page['status'] . "\n";
    // $page['html_url']     — download raw HTML
    // $page['markdown_url'] — download markdown
}

$newJob = $spidra->crawl->extract(
    $sourceJobId,
    'Extract only the product SKUs and prices as a flat list'
);

// Poll the new job
$result = $spidra->crawl->get($newJob['jobId']);

$history = $spidra->crawl->history(page: 1, limit: 10);
foreach ($history['jobs'] as $job) {
    echo "{$job['base_url']} — {$job['status']} — {$job['pages_crawled']} pages\n";
}

$stats = $spidra->crawl->stats();
echo "Total crawls: {$stats['total']}\n";

// List logs with optional filters
$result = $spidra->logs->list([
    'status'    => 'failed',       // 'success' | 'failed'
    'searchTerm'=> 'amazon.com',
    'dateStart' => '2024-01-01',
    'dateEnd'   => '2024-12-31',
    'page'      => 1,
    'limit'     => 20,
]);

foreach ($result['data']['logs'] as $log) {
    echo $log['urls'][0]['url'] . ' — ' . $log['status'] . ' — ' . $log['credits_used'] . " credits\n";
}

$log = $spidra->logs->get('log-uuid-here');
print_r($log['data']['result_data']); // full AI output for that job

// Range options: '7d' | '30d' | 'weekly'
$result = $spidra->usage->get('30d');

foreach ($result['data'] as $row) {
    echo "{$row['date']}: {$row['requests']} requests, {$row['credits']} credits, {$row['tokens']} tokens\n";
}

use Spidra\Exceptions\AuthenticationException;
use Spidra\Exceptions\InsufficientCreditsException;
use Spidra\Exceptions\RateLimitException;
use Spidra\Exceptions\ServerException;
use Spidra\Exceptions\SpidraException;

try {
    $job = $spidra->scrape->run([...]);
} catch (AuthenticationException $e) {
    // 401 — API key is missing or invalid
    echo "Check your API key\n";
} catch (InsufficientCreditsException $e) {
    // 403 — monthly credit limit reached
    echo "Out of credits. Top up at app.spidra.io\n";
} catch (RateLimitException $e) {
    // 429 — too many requests
    echo "Rate limited, back off and retry\n";
} catch (ServerException $e) {
    // 500 — something went wrong on Spidra's side
    echo "Server error, try again\n";
} catch (SpidraException $e) {
    // Any other API error
    echo "{$e->statusCode}: {$e->getMessage()}\n";
}

$spidra = new SpidraClient(
    apiKey:  'spd_YOUR_API_KEY',
    baseUrl: 'http://localhost:4321/api', // for local development
);
bash
composer