Nomisdk Posted April 19, 2014 Share Posted April 19, 2014 (edited) HiI have the following function pulling data from a API.Sadly there is a limit at the API, that i can only pull 500 entries.I cant query the API for a total number for entires.What i need help for is, how i can make it query the API and save the data, untill it has all entires. public function loadAccounts() { $startid = -1; $xml = ' <queryxml> <entity>Account</entity> <query> <field>id<expression op="greaterthan">'. $startid .'</expression></field> </query> </queryxml> '; $query = new query ( $xml ); $result = $this->client->query ( $query ); $this->accounts = Array (); $laccounts = $result->queryResult->EntityResults->Entity; foreach ( $laccounts as $account ) { $this->accounts [$account->id] = $account; } Can someone help me setting the code up, so it will re-quey untill it has all entries? Edited April 19, 2014 by Nomisdk Quote Link to comment https://forums.phpfreaks.com/topic/287887-query-and-api-limit/ Share on other sites More sharing options...
Nomisdk Posted April 20, 2014 Author Share Posted April 20, 2014 Any who can help me out? Quote Link to comment https://forums.phpfreaks.com/topic/287887-query-and-api-limit/#findComment-1476748 Share on other sites More sharing options...
DavidAM Posted April 20, 2014 Share Posted April 20, 2014 There's not enough information to give a complete answer. If you can specify an ORDER BY, so you get the Accounts in sequence you can just put that code in a loop until you get an indication that nothing was found. See my comments (#MAD) in the code below. Since I don't know the API, I can only guess at how to determine the exit point. public function loadAccounts() { $startid = -1; #MAD Initialize the collection of accounts first $this->accounts = Array (); while(true) { #MAD Until we issue a break; #MAD You need to check the API for an ORDER BY command - Here's my GUESS $xml = ' <queryxml> <entity>Account</entity> <query> <field>id<expression op="greaterthan">'. $startid .'</expression></field> <order direction="ascending">id</order> </query> </queryxml> '; $query = new query ( $xml ); $result = $this->client->query ( $query ); #MAD If the API returns FALSE when nothing is found ... if (!$result) break; #MAD OR test Entity to see if it has children? if (!$result->queryResult->EntityResults->Entity->haschildren) break; $laccounts = $result->queryResult->EntityResults->Entity; #MAD OR if $laccounts is an Array if (empty($laccounts)) break; foreach ( $laccounts as $account ) { $this->accounts [$account->id] = $account; $startid = $account; #MAD So we get the LAST one returned and can issue a query for next } } Quote Link to comment https://forums.phpfreaks.com/topic/287887-query-and-api-limit/#findComment-1476749 Share on other sites More sharing options...
Nomisdk Posted April 21, 2014 Author Share Posted April 21, 2014 (edited) Hi DavidI always get the same order returned from the API, so I dont think that the "<order direction="ascending">id</order>" is needed.The following is stated in the API User guide: query() This API call executes a query against Autotask and returns an array of matching entities. The queries are built using the QueryXML format and will return a maximum of 500 records at once, sorted by their id value.(https://www.autotask.net/help/content/Userguides/T_WebServicesAPIv1_5.pdf)I will try testing your code later today Edited April 21, 2014 by Nomisdk Quote Link to comment https://forums.phpfreaks.com/topic/287887-query-and-api-limit/#findComment-1476838 Share on other sites More sharing options...
Nomisdk Posted April 21, 2014 Author Share Posted April 21, 2014 Ok it works But i get the following notice:Notice: Undefined property: stdClass::$Entity in /var/www/Autotask/class.autotask.php on line 114Line 114 is: #MAD OR test Entity to see if it has children? if (!$result->queryResult->EntityResults->Entity) break;How to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/287887-query-and-api-limit/#findComment-1476839 Share on other sites More sharing options...
boompa Posted April 21, 2014 Share Posted April 21, 2014 (edited) In that doc you linked, under the API Best Practices heading is an entry "Use Multiple Queries to Retrieve Over 500 Records" that tells you how to do your original task. For the following, use isset() to check if that property is set? Notice: Undefined property: stdClass::$Entity in /var/www/Autotask/class.autotask.php on line 114Line 114 is:#MAD OR test Entity to see if it has children? if (!$result->queryResult->EntityResults->Entity) break;How to fix this? Edited April 21, 2014 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/287887-query-and-api-limit/#findComment-1476841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.