APIs & Integrations

TDwebdev
Contributor | Elite Partner
Contributor | Elite Partner

How to loop through all offsets en get all results from HubSpot

SOLVE

Hello,

I want loop through all companies and make one list of all 3000 companies.

 

My code:

 

    $api_key = $this->api_key();

        // Get companies

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Get result after POST
        $result = curl_exec($ch);

        // Convert the JSON result to a array
        $result_back = json_decode($result);

        $companys = $result_back->companies;

           echo "<pre>";
           print_r($result_back);
           echo "</pre>";

        return $companys;


Vet Digital

Did my post solve your question? Help the community by marking it as a solution
0 Upvotes
1 Accepted solution
TDwebdev
Solution
Contributor | Elite Partner
Contributor | Elite Partner

How to loop through all offsets en get all results from HubSpot

SOLVE

I fixed the problem with the while loop :

 

 

        $offset = 0;
        $hasMore = true;

        while ($hasMore === true) {
           //Add the API call in the while loop with the $offset in the url
            $url = "https://api.hubapi.com/deals/v1/deal/paged?Associations=true&limit=250&offset=".$offset."&hapikey=" . $api_key;
            $offset = $result_back['offset'];
            $hasMore = $result_back['hasMore'];

        }

 

 

 

 

 

 



Vet Digital

Did my post solve your question? Help the community by marking it as a solution

View solution in original post

2 Replies 2
TDwebdev
Solution
Contributor | Elite Partner
Contributor | Elite Partner

How to loop through all offsets en get all results from HubSpot

SOLVE

I fixed the problem with the while loop :

 

 

        $offset = 0;
        $hasMore = true;

        while ($hasMore === true) {
           //Add the API call in the while loop with the $offset in the url
            $url = "https://api.hubapi.com/deals/v1/deal/paged?Associations=true&limit=250&offset=".$offset."&hapikey=" . $api_key;
            $offset = $result_back['offset'];
            $hasMore = $result_back['hasMore'];

        }

 

 

 

 

 

 



Vet Digital

Did my post solve your question? Help the community by marking it as a solution
dennisedson
HubSpot Product Team
HubSpot Product Team

How to loop through all offsets en get all results from HubSpot

SOLVE

@TDwebdev 

You should get a paging object in your return.  Within that, you will use the link value to append to the uri for a new call. 

Example of the object:

"paging": {
    "next": {
      "after": "NTI1Cg%3D%3D",
      "link": "?after=NTI1Cg%3D%3D"
    }
  }
0 Upvotes