PHP Pagination

KMartin4
Participant

Hi,

Hopefully someone can give me a nudge in the right direction, I'm building a portal where users can create a 'Project' (custom object) record and associate it with other objects. I have this all functional except one issue and that is being able to get more than 100 results in an api call.

I know the Searhc API allows up to 10,000 using the pagination feature but I'm looking to see if anyone has any guidence on a good method to loop through all the results using PHP.

 

Below is an example of an api call for me searching for all 'Projects' that have an id. This returns the first 100 perfectly. I'm then adding the projects into a 'Table'.

Looking for some help on how i'd be able to loop through calling the api until I have all the projects?

   
    $curl = curl_init();
    curl_setopt_array($curlarray(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
        "limit" : 100,
        "filters": [
        {
            "propertyName": "project_id",
            "operator": "HAS_PROPERTY"
        }
        ],
        "properties": [ "project_name" ]
    }',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
    ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $json = json_decode($response);
    $results = $json->results;
PHP
0 Upvotes
4 Replies 4
natsumimori
Community Manager

Hi @KMartin4 ,

 

Let me tag in some Community top contributors here:

@JBeatty and @himanshurauthan , is this something you're familiar with? Would you be able to weigh in here?

0 Upvotes
KMartin4
Participant

This might not be the cleanest way, but I think I managed to get the outcome I am after by building this function. It loops through and recalls the api if there is an 'after' value in the result of the previous api call.

It then adds a '<td>' to a table to show all projects.

function getProjects($count = 0, $after = 0){

    ${'curl' . $count} = curl_init();
    curl_setopt_array(${'curl' . $count}, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
        "after": '.$after.',
        "limit": 100,
        "filters": [
        {
            "propertyName": "project_id",
            "operator": "HAS_PROPERTY"
        }
        ],
        "properties": [ "project_name", "hs_createdate" ]
    }',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
    ),
    ));
    ${'reponse' . $count} = curl_exec(${'curl' . $count});
    curl_close(${'curl' . $count});
    ${'json' . $count} = json_decode(${'reponse' . $count});
    
    
    if (isset(${'json' . $count}->results)){
        ${'results' . $count} = ${'json' . $count}->results;
    }else{
        ${'results' . $count} = 'N/A';
    }

    if (isset(${'json' . $count}->paging)){
        ${'paging' . $count} = ${'json' . $count}->paging;
    }else{
        ${'paging' . $count} = 'N/A';
    }

    if(${'results' . $count} !== 'N/A'){

        foreach(${'results' . $count} as $result){
            // print_r($result);
            $newDate = date("d/m/Y"strtotime($result->properties->hs_createdate));
            ?>
            <tr>
                <th scope="row"><?php if (isset($result->properties->project_name)){echo $result->properties->project_name;}else{echo 'N/A';} ?></th>
                <td class='dates' scope="row"><?php if (isset($result->properties->hs_createdate)){echo $newDate;}else{echo 'N/A';} ?></td>
            </tr>
            <?php
        }
    }

    if(${'paging' . $count} !== 'N/A'){
        $count++;
        $after++;
        $after = $after + 9;
        usleep(500);
        getProjects($count$after);
    }

}


Would still be good to see some opinions on this and if there is a cleaner better method to acheive this.

I had to add a usleep() to ensure it doesnt hit the api call limit
natsumimori
Community Manager

@Teun and @Indra (Sorry, I mention you too often!)😓

 

I'm wondering if you are familiar with this topic??

Teun
Guide | Diamond Partner

Hi @natsumimori ,

 

I'll probably be able to test some solutions next week or the week after. 

Did my answer solve your issue? Help the community by marking it as the solution.