PHP code example of husail / http-iterator

1. Go to this page and download the library: Download husail/http-iterator 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/ */

    

husail / http-iterator example snippets


use Husail\HttpIterator\HttpIterator;
use Illuminate\Support\Facades\Http;

$perPage = 10;
$startPage = 1;
HttpIterator::run($perPage, $startPage, function ($iterator) {
     $response = Http::get('https://example.com/api/transactions', [
        'page' => $iterator->currentPage(),
        'per_page' => $iterator->perPage(),
    ]);

    if (!$iterator->hasInitialized()) {    
        $iterator->setTotalResult($response->json('total_elements'));
    }

    TransactionRepository::sync($response->json('data'))
    //...
       
    $iterator->nextPage();
});

use Husail\HttpIterator\HttpIterator;
use Illuminate\Support\Facades\Http;

const THROTTLE_DELAY_SECONDS = 60;

$perPage = 10;
$startPage = 1;
HttpIterator::run($perPage, $startPage, function ($iterator) {
     $response = Http::get('https://example.com/api/transactions', [
        'page' => $iterator->currentPage(),
        'per_page' => $iterator->perPage(),
    ]);
  
    if ($response->status() === 429) {
        sleep(THROTTLE_DELAY_SECONDS);
        $iterator->retry();
    }

    if (!$iterator->hasInitialized()) {    
        $iterator->setTotalResult($response->json('total_elements'));
    }

    TransactionRepository::sync($response->json('data'))
    //...
       
    $iterator->nextPage();
});