API tip - Fetch multiple pages of results automatically

Have more questions? Submit a request

8 Comments

  • Pierugo

    Hi,

    I have written a VBA macro to consume retrieve tickets but I am a bit stuck to check and get more than 1 page when records are more than 100.

    Can someone help please?

    0
  • Daniel Cooper

    @Pierugo, 

    I'm not familiar with VBA, but for most APIs when you need to go to the next page, you can do so by appending "?page=#" to the end of the URL. For example, for the user endpoint try https://subdomain.zendesk.com/api/v2/users.json?page=2 to pull the second set of 100 users.  Within your VBA macro if you configure it to iterate through multiple pages it should give you what you need. 

    Some APIs also let you adjust the amount of records per page.  Add "?per_page=#" to the end of the URL to control the records per page.  I haven't seen this allow more than 100 records at a time, but if you are working on data that takes a while to pull (attachments for example) sometimes it helps to restrict how many records are on a page. 

    0
  • Pierugo

    Thanks Daniel.

    The problem I am facing really is to get my macro to stop the iteration I set up when the "next_page" object is null, so there are no more pages to request (in case there are more than 100 records). It's more related to VBA scripting and JSON parsing.

    I set the condition 'If JSON("next_page") <> "" then' expecting VBA to recognise "" as null so I can exit the "do loop" function but that doesn't seem to be working.

    I'll keep trying...

     

    0
  • Graeme Carmichael

    If it helps. I have the web response in an object Response and check for the last page using:

    • IF IsNull (Response.Data("next_page")) then GameOver

     

    3
  • Pierugo

    Thanks Graeme, that works a treat! :-)

    I had managed to get a similar result using Do While Loop statement

    Do While JSON("next_page") <> "Null" 

    This seems to be working too. Not sure which one to go for now! :-)

    0
  • Harshal Bhavsar

    Hello everyone,

    I made changes as mentioned in the comments of the script and tried running the php script on windows using the Wamp Server, it does create the results.json file but does not include any data. It gives the below errors:

     

    1) array_merge():Argument #1 is not an array in paginate.php on line 39

                          $array = array_merge($array, $results->$resource);

    2) undefined property:stdClass::$next_page in paginate.php on line 40

                          $url = $results->next_page

        undefined property:stdClass::$user in paginate.php on line 39

                         $array = array_merge($array, $results->$resource);

    3) Trying to get property of non-object in paginate.php on line 39 and 40

                         $array = array_merge($array, $results->$resource);
                          $url = $results->next_page;

     

    I do not have enough background in PHP to debug these errors. I would appreciate any help on this, if possible.

    Thanks

    0
  • Mike Martello

    A little tip for anyone still using this script: with Zendesk's new DDoS protections, you will need to change CURLOPT_SSL_VERIFYHOST from 0 to 1.

    0
  • Jessie - Community Manager

    Thanks for sharing that info, Mike!

    0

Please sign in to leave a comment.

Powered by Zendesk