Hubspot API: Getting all contacts (100+)










1















I'm trying to create one big json file with all my contacts. Since the API returns max 100 contacts I have to work with pagination.



I've found an older topic on this but this uses an external repo. I've found a newer repo, but can't get that one to work. Best case, I don't use an external lib because I need such a small piece of code.



I've tried the following code, but it keeps on loading. My guess is that the variables don't update. What am I doing wrong?



<?php


echo '<pre>';
function getData($offset = 0)

$properties = "&property=email&property=firstname&property=funnel&property=hs_lead_status&property=start_session";
$apikey = "xxxx-xx-xx";
$feed_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=". $apikey. $properties.'&vidOffset='.$offset;

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $feed_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$data = json_decode($response);

return $data; //return the results for use


$allData = array();




$offset = 0;
$hasMore = true;

while ($hasMore === true)

$response = getData( $offset );
$allData = $response;


$offset = $response->'vid-offset';
$hasMore = $response->'has-more';

var_dump( $hasMore );
$hasMore = false;










share|improve this question


























    1















    I'm trying to create one big json file with all my contacts. Since the API returns max 100 contacts I have to work with pagination.



    I've found an older topic on this but this uses an external repo. I've found a newer repo, but can't get that one to work. Best case, I don't use an external lib because I need such a small piece of code.



    I've tried the following code, but it keeps on loading. My guess is that the variables don't update. What am I doing wrong?



    <?php


    echo '<pre>';
    function getData($offset = 0)

    $properties = "&property=email&property=firstname&property=funnel&property=hs_lead_status&property=start_session";
    $apikey = "xxxx-xx-xx";
    $feed_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=". $apikey. $properties.'&vidOffset='.$offset;

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => $feed_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
    ),
    ));
    $response = curl_exec($curl);
    $data = json_decode($response);

    return $data; //return the results for use


    $allData = array();




    $offset = 0;
    $hasMore = true;

    while ($hasMore === true)

    $response = getData( $offset );
    $allData = $response;


    $offset = $response->'vid-offset';
    $hasMore = $response->'has-more';

    var_dump( $hasMore );
    $hasMore = false;










    share|improve this question
























      1












      1








      1








      I'm trying to create one big json file with all my contacts. Since the API returns max 100 contacts I have to work with pagination.



      I've found an older topic on this but this uses an external repo. I've found a newer repo, but can't get that one to work. Best case, I don't use an external lib because I need such a small piece of code.



      I've tried the following code, but it keeps on loading. My guess is that the variables don't update. What am I doing wrong?



      <?php


      echo '<pre>';
      function getData($offset = 0)

      $properties = "&property=email&property=firstname&property=funnel&property=hs_lead_status&property=start_session";
      $apikey = "xxxx-xx-xx";
      $feed_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=". $apikey. $properties.'&vidOffset='.$offset;

      $curl = curl_init();
      curl_setopt_array($curl, array(
      CURLOPT_URL => $feed_url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
      "cache-control: no-cache"
      ),
      ));
      $response = curl_exec($curl);
      $data = json_decode($response);

      return $data; //return the results for use


      $allData = array();




      $offset = 0;
      $hasMore = true;

      while ($hasMore === true)

      $response = getData( $offset );
      $allData = $response;


      $offset = $response->'vid-offset';
      $hasMore = $response->'has-more';

      var_dump( $hasMore );
      $hasMore = false;










      share|improve this question














      I'm trying to create one big json file with all my contacts. Since the API returns max 100 contacts I have to work with pagination.



      I've found an older topic on this but this uses an external repo. I've found a newer repo, but can't get that one to work. Best case, I don't use an external lib because I need such a small piece of code.



      I've tried the following code, but it keeps on loading. My guess is that the variables don't update. What am I doing wrong?



      <?php


      echo '<pre>';
      function getData($offset = 0)

      $properties = "&property=email&property=firstname&property=funnel&property=hs_lead_status&property=start_session";
      $apikey = "xxxx-xx-xx";
      $feed_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=". $apikey. $properties.'&vidOffset='.$offset;

      $curl = curl_init();
      curl_setopt_array($curl, array(
      CURLOPT_URL => $feed_url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
      "cache-control: no-cache"
      ),
      ));
      $response = curl_exec($curl);
      $data = json_decode($response);

      return $data; //return the results for use


      $allData = array();




      $offset = 0;
      $hasMore = true;

      while ($hasMore === true)

      $response = getData( $offset );
      $allData = $response;


      $offset = $response->'vid-offset';
      $hasMore = $response->'has-more';

      var_dump( $hasMore );
      $hasMore = false;







      api loops while-loop hubspot






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 10:45









      IljaIlja

      62




      62






















          1 Answer
          1






          active

          oldest

          votes


















          0














          One thing that I can think of is that your loop doesn't take into consideration the fact that HubSpot only allows 10 request per second (see here) - your loop might be exceeding that and as such causing the issues. You should add the following code just before the end of your while loop (this will sleep the script for 0.1 seconds):



          usleep(100000);


          Additionally, it might be a better and a cleaner idea to use a PHP API library such as this one.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279264%2fhubspot-api-getting-all-contacts-100%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            One thing that I can think of is that your loop doesn't take into consideration the fact that HubSpot only allows 10 request per second (see here) - your loop might be exceeding that and as such causing the issues. You should add the following code just before the end of your while loop (this will sleep the script for 0.1 seconds):



            usleep(100000);


            Additionally, it might be a better and a cleaner idea to use a PHP API library such as this one.






            share|improve this answer



























              0














              One thing that I can think of is that your loop doesn't take into consideration the fact that HubSpot only allows 10 request per second (see here) - your loop might be exceeding that and as such causing the issues. You should add the following code just before the end of your while loop (this will sleep the script for 0.1 seconds):



              usleep(100000);


              Additionally, it might be a better and a cleaner idea to use a PHP API library such as this one.






              share|improve this answer

























                0












                0








                0







                One thing that I can think of is that your loop doesn't take into consideration the fact that HubSpot only allows 10 request per second (see here) - your loop might be exceeding that and as such causing the issues. You should add the following code just before the end of your while loop (this will sleep the script for 0.1 seconds):



                usleep(100000);


                Additionally, it might be a better and a cleaner idea to use a PHP API library such as this one.






                share|improve this answer













                One thing that I can think of is that your loop doesn't take into consideration the fact that HubSpot only allows 10 request per second (see here) - your loop might be exceeding that and as such causing the issues. You should add the following code just before the end of your while loop (this will sleep the script for 0.1 seconds):



                usleep(100000);


                Additionally, it might be a better and a cleaner idea to use a PHP API library such as this one.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 7 '18 at 13:00









                itoctopusitoctopus

                3,34342236




                3,34342236





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279264%2fhubspot-api-getting-all-contacts-100%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo