PHP

Consume a Paginated REST API Efficiently with Guzzle in PHP

Master fetching and processing paginated data from RESTful APIs in PHP using the Guzzle HTTP client, including iterating through multiple pages.

<?php
require 'vendor/autoload.php'; // Ensure Guzzle is loaded

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

function fetchAllPaginatedData(string $initialUrl, array $options = []): array
{
    $client = new Client();
    $allData = [];
    $currentPageUrl = $initialUrl;

    while ($currentPageUrl !== null) {
        try {
            echo "Fetching: " . $currentPageUrl . "
";
            $response = $client->request('GET', $currentPageUrl, $options);
            $statusCode = $response->getStatusCode();

            if ($statusCode >= 200 && $statusCode < 300) {
                $body = json_decode($response->getBody()->getContents(), true);

                // Adjust these keys based on your API's response structure
                // Common patterns: 'data' for items, 'next_page_url' or 'links.next' for pagination
                $items = $body['data'] ?? [];
                $nextPageUrl = $body['next_page_url'] ?? null;
                
                // Fallback for APIs using a 'links' object (e.g., JSON:API)
                if (!$nextPageUrl && isset($body['links']['next'])) {
                    $nextPageUrl = $body['links']['next'];
                }

                $allData = array_merge($allData, $items);
                $currentPageUrl = $nextPageUrl;
            } else {
                echo "API returned status code: " . $statusCode . "
";
                break; // Stop on non-success status codes
            }
        } catch (GuzzleException $e) {
            echo "Error fetching data: " . $e->getMessage() . "
";
            break; // Stop on network or client errors
        }
    }

    return $allData;
}

// --- Example Usage ---
// Assumes an API that returns data like:
// {
//   "data": [{...}, {...}],
//   "next_page_url": "https://api.example.com/items?page=2"
// }
// Or for JSON:API style:
// {
//   "data": [{...}, {...}],
//   "links": {
//     "next": "https://api.example.com/items?page=2"
//   }
// }

// $initialApiUrl = 'https://api.example.com/items?page=1'; // Replace with your API endpoint
// $authHeaders = ['headers' => ['Authorization' => 'Bearer your_token_here']]; // Example for auth

// try {
//     $result = fetchAllPaginatedData($initialApiUrl, $authHeaders);
//     echo "Fetched " . count($result) . " items across all pages.
";
//     // print_r($result); // Uncomment to see all fetched data
// } catch (Exception $e) {
//     echo "Operation failed: " . $e->getMessage() . "
";
// }
?>
How it works: This PHP snippet demonstrates how to efficiently consume data from a paginated REST API using the Guzzle HTTP client. Many APIs return large datasets in pages to manage server load and network bandwidth. The `fetchAllPaginatedData` function iteratively fetches each page, aggregating the results until no further pages are indicated by the API's response (e.g., via a `next_page_url` field or a `links.next` property). This ensures all available data is retrieved seamlessly, abstracting away the manual pagination logic from your main application code.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs