Example 4 - Pagination

Introduction and data structure

 

Starting with version 1.18 of the API, it is possible to export data through a paging system. In this way, the products will be sent in blocks by predefined quantity (page) and not all at the same time as before.

This tool is excellent for companies with several products as the server will not be overloaded when exporting Sales Layer data.

Once again, we are going to use the connector created in Example 1, with the same table configuration (see table configuration in Example 1) and the same filters and parameters:

1

In this example we set all our items as visible in the PIM account.

We have 14 visible categories:

2

..and 225 products:

3

Regarding the variants, we have 16 visible elements:

4

 

Code

 

To export our data, we start by creating a  SalesLayer_Conn() class with the connector's credentials as parameters. These credentials can be found in the Sales Layer connector and have been saved in the $connector_id and $secret_key parameters (see Example 1).

When making an API call, the default version used is 1.17. As the pagination is in version 1.18 we have to define it in the code through the function set_API_version ('1.18').

Now we can define the number of data per page that we want to export. As in this example the data amount is not very large, we choose to export 50 products per page through the call set_pagination (50).

As we want to export all the data that we have in our PIM, we call the get_info () function with the last_update parameter to 0.

Through the has_response_error() function we can check if the API has returned any error, and we can now print the following relevant information using these SDK functions::

  • The API version through the get_response_api_version() function
  • UNIX date of the exact time of the API call with the get_response_time() function
  • The default language of the PIM by calling the get_response_default_language() function

Thanks to the Nice_r library (more info here) we can now print the data returned by the API with the get_response_table_data() function.

<?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>

 

Execution and its results

 

Executing the code, we obtain the following result:

5

The API has returned 14 categories and 36 products, putting together the 50 products on the first page. 

To export the next page, the API uses a public parameter called response_next_page. This contains the URL of the next page.

If we add the printing of the next parameter:echo $SLConn->response_next_page, making the script call we will see the URL:

6

However, the SDK contains functions to verify and print the following pages:

  • have_next_page(): It checks if there is one more page to print
  • get_next_page_info(): It exports the information of the next page and saves it locally

Therefore, a small script could be added to the code to automatically print the following pages:

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

In this script, we are printing the pages with the get_response_table_data() function, saving them locally with the get_next_page_info() function while there is a next page with the verification of the have_next_page () function.

Executing the code again, we would get a list of the tables for each printed table:

7