Trying to pull down all of companies

Hello, I’m currently doing a GET request to try to pull down all the companies data with getCompanies() for a client who is using a Free account, but there seems to be a limit to how many i can pull and the client has 492 companies. Is there a way to pull all of that down?

Hi @ajen_madden :waving_hand:

There should be a pagination option for you to use in this case. For HubSpot’s CRM V3 {Object} API endpoints, the maximum number of records that can be returned per “list records” call is 100

(controlled by the “limit” querystring parameter)

. Where there are more than 100 records in the given HubSpot {Object} database, you are required to paginate through the results.

The exact way to perform this pagination will likely depend on how you’re interacting with HubSpot APIs. You should find the information you need within HubSpot’s API documentation. If you’re still having trouble, please share more information about how you’re currently interacting with HubSpot APIs (e.g. cURL, Python client-library, PHP client-library).

I hope this proves useful. Please let me know if you have any follow-up questions.

Can you send me an example of how to use the pagination option? I am getting the feed using PHP with this code below:

function getCompanies() {

 try {

 $args = array(
 'headers' => array(
 'user-agent' => '',
 'Content-Type' => 'application/json',
 'Authorization' => 'Bearer ' . $this->token,
 ),
 ); 
 // there is a list of properties outside this function
 $properties = implode(',', $this->properties);

 $paging_after = get_transient('paging_after');

 $after = "";
 if($paging_after) {
 $after = "&after=".$paging_after ;
 }

 $response = wp_remote_get( $this->url.'/crm/v3/objects/companies/?limit=100&properties='.$properties.$after, $args );

 if (is_wp_error($response)) {
 $response = rest_ensure_response(array('error' => $response->get_error_message()));
 return $response->data;
 }

 $response_code = wp_remote_retrieve_response_code($response);
 $response_data = wp_remote_retrieve_body($response);

 $response = rest_ensure_response(array('code' => $response_code, 'data' => $response_data));

 return $response->data;

 } catch( Exception $ex ) {

 $response = rest_ensure_response(array('error' => 'An error occurred.'));

 return $response->data;

 }

 wp_die();
 }

Hi @ajen_madden :waving_hand:

PHP is far from my language of choice :laughing: Regardless, my gut tells me you might want to interrogate this line:

$paging_after = get_transient('paging_after');

The reason I say this is because I don’t recognise the significance of “paging_after”. There’s a chance I’m completely misunderstanding what the get_transient() function does, so please let me know if this is the case.

Below is an example response from a GET request to: https://api.hubapi.com/crm/v3/objects/COMPANY?limit=1

{
 "results": [
 {
 "id": "5077968620",
 "properties": {
 "createdate": "2021-12-22T15:41:24.878Z",
 "domain": "test-1.com",
 "hs_lastmodifieddate": "2023-08-24T22:00:07.711Z",
 "hs_object_id": "5077968620",
 "name": "Test Company 1"
 },
 "createdAt": "2021-12-22T15:41:24.878Z",
 "updatedAt": "2023-08-24T22:00:07.711Z",
 "archived": false
 }
 ],
 "paging": {
 "next": {
 "after": "5077968621",
 "link": "https://api.hubapi.com/crm/v3/objects/COMPANY?limit=1&after=5077968621"
 }
 }
}

Where relevant, the “paging” object is included in the response. The URL within the “link” object key (i.e. paging.next.link) is the request URL you should use to retrieve the next page of results. When there’s no longer any more results to page through, the “paging” object is not included in the response.

I hope this proves useful. Please let me know if you have any follow-up questions.

Thanks, Zach!

I almost have it but I put the request happening in a while loop, but it gets stuck in an infinite loop. Do you recommend a different condition?

function getCompanies() {
 try {
 $args = array(
 'headers' => array(
 'user-agent' => '',
 'Content-Type' => 'application/json',
 'Authorization' => 'Bearer ' . $this->token,
 ),
 );

 // List of properties outside this function
 $properties = implode(',', $this->properties);

 $allCompanies = array();
 // Start with an initial page number
 $page = 0;

 while (true) {
 $paging_after = get_transient('paging_after');
 $after = "";
 if ($paging_after) {
 $after = "&after=" . $paging_after;
 }
 $response = wp_remote_get($this->url . "/crm/v3/objects/companies/?limit=100&properties=$properties$after", $args);
 if (is_wp_error($response)) {
 $response = rest_ensure_response(array('error' => $response->get_error_message()));
 return $response->data;
 }
 $response_code = wp_remote_retrieve_response_code($response);
 $response_data = wp_remote_retrieve_body($response);
 // Check if the response_data is already an array, and if so, use it directly
 $companies = is_array($response_data) ? $response_data : json_decode($response_data, true);
 if (empty($companies)) {
 break; // No more companies to fetch
 }
 $allCompanies = array_merge($allCompanies, $companies);
 // Update the 'paging_after' transient for the next page
 $response_headers = wp_remote_retrieve_headers($response);
 if (isset($response_headers['x-paging-after'])) {
 set_transient('paging_after', $response_headers['x-paging-after'], 3600); // Store for one hour
 } else {
 delete_transient('paging_after'); // Clear transient if no 'x-paging-after' header
 }
 // Increment the page number for the next request
 $page++;
 }
 return $allCompanies;
 } catch (Exception $ex) {
 $response = rest_ensure_response(array('error' => 'An error occurred.'));
 return $response->data;
 }
 wp_die();
}

We are also trying another way with this function below but it is also only pulling in 100 only.

function getAllCompanies($privateToken, $url = null, $allCompanies = array()) {
 if ($url === null) {
 // Initial request
 $properties = implode(',', $this->properties);

 $paging_after = get_transient('paging_after');
 $after = "";
 if ($paging_after) {
 $after = "&after=" . $paging_after;
 }

 $url = "https://api.hubapi.com/crm/v3/objects/companies/?limit=100&properties=$properties$after";
 }

 $response = wp_remote_get($url, array(
 'headers' => array(
 'Authorization' => 'Bearer ' . $privateToken,
 ),
 ));

 if (is_wp_error($response)) {
 // Handle the error as needed
 return array();
 }

 $response_code = wp_remote_retrieve_response_code($response);
 $response_data = wp_remote_retrieve_body($response);
 // Check if the response_data is already an array, and if so, use it directly
 $responseArray = is_array($response_data) ? $response_data : json_decode($response_data, true);

 if (isset($responseArray['results'])) {
 $companies = $responseArray['results'];
 $allCompanies = array_merge($allCompanies, $companies);
 }

 // Check if there are more pages
 if (isset($responseArray['hasMore']) && $responseArray['hasMore']) {
 $nextPageUrl = $responseArray['next']['link'];
 return getAllCompanies($privateToken, $nextPageUrl, $allCompanies);
 }

 return $allCompanies;
}

Not sure what we’re doing wrong but any suggestion is greatly appreciated!

Hi @ajen_madden :waving_hand:

Happy to at least try to help :slightly_smiling_face:

Again, PHP is not my language of choice. Regardless, I’ll give it a shot. You may want to try something resembling the below function. Note that there’s some duplicate code

(when making the API requests)

that could be made more efficient. I left it as is due to time constraints and my own lack of PHP skill! I’ve also not tested this, mainly because I don’t have access to the WP functions

(e.g. wp_remote_get)

.

(I’ve explained my edits in code comments preceded with **)

// ** I deleted the 2nd argument, 'url'. I think a 'while' loop could be a better solution here, rather than recursively calling the function

function getAllCompanies($privateToken, $allCompanies = array()) {
 // Initial request
 // ** I deleted references to pagination here, because the first request will never require the 'after' querystring parameter
 $properties = implode(',', $this->properties);
 $url = "https://api.hubapi.com/crm/v3/objects/companies/?limit=100&properties=$properties";

 $response = wp_remote_get($url, array(
 'headers' => array(
 'Authorization' => 'Bearer ' . $privateToken,
 ),
 ));

 if (is_wp_error($response)) {
 // Handle the error as needed
 return array();
 }

 $response_code = wp_remote_retrieve_response_code($response);
 $response_data = wp_remote_retrieve_body($response);
 // Check if the response_data is already an array, and if so, use it directly
 $responseArray = is_array($response_data) ? $response_data : json_decode($response_data, true);

 if (isset($responseArray['results'])) {
 $companies = $responseArray['results'];
 $allCompanies = array_merge($allCompanies, $companies);
 }

 // Check if there are more pages
 // ** I changed the key check here from 'hasMore' to 'paging' and removed the second condition because I think the first alone will cover what you are trying to achieve
 // ** I also changed this to a while loop
 while (isset($responseArray['paging'])) {
 $nextPageUrl = $responseArray['paging']['next']['link'];

 $response = wp_remote_get($nextPageUrl, array(
 'headers' => array(
 'Authorization' => 'Bearer ' . $privateToken,
 ),
 ));

 if (is_wp_error($response)) {
 // Handle the error as needed
 return array();
 }

 $response_code = wp_remote_retrieve_response_code($response);
 $response_data = wp_remote_retrieve_body($response);
 // Check if the response_data is already an array, and if so, use it directly
 $responseArray = is_array($response_data) ? $response_data : json_decode($response_data, true);

 if (isset($responseArray['results'])) {
 $companies = $responseArray['results'];
 $allCompanies = array_merge($allCompanies, $companies);
 }
 }

 return $allCompanies;
}

Hopefully, that’s a step in the right direction for your use case :crossed_fingers:

Please let me know if you have any follow-up questions.