4 - Pagination

Modified on Tue, 19 May at 11:57 AM

Introduction and data structure


Starting with API version 1.18, data can be exported using a pagination system. This means products are returned in pages of predefined size instead of all at once.

This is especially useful when working with large catalogs because it helps avoid overloading the server during export.

This example uses the same connector configured in Example 1.



In this scenario, all items are set to visible: 14 categories, 225 products, and 16 variants.





Code


The script starts by creating a SalesLayer_Conn() object with the connector credentials. Since pagination is available in version 1.18, the first step is to define the API version with set_API_version('1.18').

Then the page size is set with set_pagination(50), which means the export returns 50 products per page.

Because this is a full export, last_update is set to 0.

<?php 
define('LOC_BASE', dirname(__FILE__) . '/');
require(LOC_BASE.'SalesLayer-Conn.php');
require(LOC_BASE.'lib/nice_r-master/Nicer.php');
?>

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
    <link rel="stylesheet" type="text/css" href="lib/nice_r-master/nice_r.css?version=<?php echo filemtime(LOC_BASE.'lib/nice_r-master/nice_r.css'); ?>"/>
    <script type="text/javascript" src="lib/nice_r-master/nice_r.js?version=<?php echo filemtime(LOC_BASE.'lib/nice_r-master/nice_r.js'); ?>"></script>
</head>
<body>
    <?php
    //SL connector credencials
    $connector_id = 'CN12347H3308C4486';
    $secret_key = '7aeb575d9bf15bfa238e8f01842417a2';

    $last_update= "0";

    //Create object with the credentials on the connector in SL
    $SLConn = new SalesLayer_Conn ($connector_id, $secret_key);

    //Define the version of the API to point
    $SLConn->set_API_version('1.18');
    //Define the number of the pagination
    $SLConn->set_pagination(50);
    
    //API call
    $SLConn->get_info($last_update);

    if ($SLConn->has_response_error()) {
        echo "<h4>Error:</h4>\n\n Code: ".$SLConn->get_response_error().
             "<br>\nMessage: ".           $SLConn->get_response_error_message();
    } else {
        echo "<h4>Response OK</h4>\n".
             "<p>".
             "API version: <b>".            $SLConn->get_response_api_version()          ."</b><br />\n".
             "Time: <b>".                   $SLConn->get_response_time('unix')                 ."</b><br/>\n".
             "Default language: <b>".       $SLConn->get_response_default_language()     ."</b><br/><br />\n".
             "</p>";

        //Print API response
        $n = new Nicer($SLConn->get_response_table_data());
        $n->render();
        echo "<hr/>";
    }       
    ?>
</body>
</html>

The example also shows how to request the next page by using the new last_update and page values returned by the previous response.

do {
    $SLConn->get_next_page_info();
    $n = new Nicer($SLConn->get_response_table_data());
    $n->render();
    echo "<hr/>";
} while ($SLupdate->have_next_page());


Execution and results


The first call returns the first page of products, together with the response metadata for pagination.



The response includes the page data, the connector response time, and the values needed to fetch the next page.

When the next call is made with those values, the API returns the following page instead of repeating the first one.



By repeating the process, you can continue reading the export in blocks until the connector has returned all available data.




Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article