PHP code example of rkdhatterwal / decodo-scraper

1. Go to this page and download the library: Download rkdhatterwal/decodo-scraper 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/ */

    

rkdhatterwal / decodo-scraper example snippets


use Rkdhatterwal\DecodoScraper\Facades\Decodo;

// Simple scrape → ScrapeResult
$result = Decodo::scrape('https://example.com');
echo $result->content;     // raw HTML
echo $result->statusCode;  // upstream HTTP status

// JavaScript rendering
$result = Decodo::scrapeWithJs('https://example.com');

// Screenshot (PNG)
$result = Decodo::screenshot('https://example.com');

// Geo-targeted
$result = Decodo::scrapeFromGeo('https://example.com', 'United States');

// Return Markdown (great for LLM pipelines)
$result = Decodo::scrapeAsMarkdown('https://example.com');

// Structured data via a target template's parser
$result = Decodo::scrapeWithParser('amazon_pricing', 'https://amazon.com/dp/B0BS1QCF');

// Scrape multiple URLs at once
$results = Decodo::scrapeMany(['https://example.com', 'https://another.com']);

use Rkdhatterwal\DecodoScraper\PayloadBuilder;
use Rkdhatterwal\DecodoScraper\Facades\Decodo;

$results = Decodo::send(
    (new PayloadBuilder())
        ->url('https://example.com')
        ->headless('html')
        ->geo('Germany')
        ->locale('de-DE')
        ->deviceType('mobile')
        ->proxyPool('premium')
        ->markdown()
        ->successfulStatusCodes([200, 301])
);

$product = Product::find(1);
DecodoAsync::queueTask('https://example.com', scrapeable: $product);

// Later retrieve it
$task = $product->decodoTasks()->latest()->first();

use Rkdhatterwal\DecodoScraper\Facades\DecodoAsync;

// Queue and get a task ID immediately
$task = DecodoAsync::queueTask('https://example.com');
echo $task->id;      // "7434928397127555073"
echo $task->status;  // "pending"

// With a webhook callback
$task = DecodoAsync::queueTask(
    url:         'https://example.com',
    options:     ['headless' => 'html', 'geo' => 'United States'],
    callbackUrl: 'https://my.app/webhook/decodo',
    passthrough: 'my-verification-secret',   // echoed back for auth
);

use Rkdhatterwal\DecodoScraper\PayloadBuilder;
use Rkdhatterwal\DecodoScraper\Facades\DecodoAsync;

$task = DecodoAsync::queueTaskWithBuilder(
    (new PayloadBuilder())
        ->url('https://example.com')
        ->headless('html')
        ->geo('United States')
        ->markdown()
);

$batch = DecodoAsync::queueBatch(
    urls:        ['https://site1.com', 'https://site2.com', 'https://site3.com'],
    options:     ['geo' => 'United States'],
    callbackUrl: 'https://my.app/webhook/decodo-batch',
    batchName:   'Weekly SEO Audit', // Optional name for easier tracking
);

$batch->id;      // Internal batch ID (v3)
$batch->tasks;   // Collection of TaskResponse DTOs
$batch->ids();   // Collection of task IDs
$batch->count(); // 3

// Poll status manually
$status = DecodoAsync::getTaskStatus($task->id);
$status->isPending();  // true / false
$status->isDone();     // true / false
$status->isFaulted();  // true / false

// Retrieve results once done (valid for 24 hours)
$results = DecodoAsync::getTaskResults($task->id);  // Collection<ScrapeResult>
$result  = DecodoAsync::getFirstTaskResult($task->id);  // ScrapeResult

// Convenience: poll and block until done (for scripts/queues)
$results = DecodoAsync::pollUntilDone($task->id, intervalMs: 2000, maxAttempts: 30);

$payload = (new PayloadBuilder())
    ->geo('United States')
    ->buildBatch(['https://site1.com', 'https://site2.com']);

   'decodo/webhook/*'
   

'cache' => [
    'enabled' => true,
    'ttl' => 82800,
],

'logging' => [
    'channel' => 'decodo',
],

'decodo' => [
    'driver' => 'daily',
    'path'   => storage_path('logs/decodo.log'),
    'level'  => 'debug',
    'days'   => 14,
],

// Example: notify when a batch finishes
Event::listen(DecodoBatchCompleted::class, function ($event) {
    Log::info("Batch {$event->batch->id} is done!");
});

'pruning' => [
    'content_days'       => 7,  // Keep raw HTML for 7 days
    'tasks_days'         => 30, // Keep task records for 30 days
    'pending_tasks_days' => 3,  // Delete abandoned tasks after 3 days
    'batches_days'       => 60, // Keep batch records for 60 days
    'schedule_enabled'   => true,
    'schedule_frequency' => 'daily', // Laravel scheduler frequency
],

$result->isSuccessful(); // true when statusCode is 2xx
$result->toArray();

$task->id;          // Task ID for later retrieval
$task->status;      // "pending" | "done" | "faulted"
$task->isPending(); // bool
$task->isDone();    // bool
$task->isFaulted(); // bool
$task->toArray();

$batch->id;         // Batch ID (v3)
$batch->tasks;      // Collection of TaskResponse DTOs
$batch->ids();       // Collection of task IDs
$batch->count();    // Total task count
$batch->toArray();

use Rkdhatterwal\DecodoScraper\Testing\DecodoFake;

$fake = DecodoFake::make()->swap();

// Stub a response
$fake->fakeScrape('<html>Hello World</html>');

// Act
$result = Decodo::scrape('https://example.com');

// Assert
$fake->assertScraped('https://example.com');
$this->assertEquals('<html>Hello World</html>', $result->content);

$fake->fakeTask('task-123');
DecodoAsync::queueTask('https://example.com');

$fake->assertTaskQueued('https://example.com');

$fake->fakeBatch(['task-1', 'task-2']);
DecodoAsync::queueBatch(['https://a.com', 'https://b.com']);

$fake->assertBatchQueued(2); // asserts batch with 2 URLs was queued

$fake->assertNotScraped('https://example.com');
$fake->assertScrapeCount(5);
$fake->assertTaskNotQueued('https://example.com');
$fake->assertTaskQueuedCount(3);
$fake->assertBatchQueuedCount(1);
$fake->assertNothingSent();

// Access recorded calls directly
$scrapes = $fake->recordedScrapes();
$tasks   = $fake->recordedTasks();
bash
php artisan vendor:publish --tag=decodo-config
php artisan vendor:publish --tag=decodo-migrations
php artisan migrate