Hey all! I’m actually new to PHP – I just started learning recently in order to integrate my website with an API. So far it’s gone very well, however I’ve been unable to find information relating to my problem.
The goal: to use an external API to pull “Orders,” “Products” and “Packages.” After this, allow exporting a list of the logged in user’s orders (past and active), along with all important information.
What works: it pulls the information and posts it.
What doesn’t: the API uses “pagination” and I’m unable to get past that limitation. As an example, there are currently 22 orders. If I call the API it will send the first 10, and they are output flawlessly. To get more I can append “per_page” and “page=#” to the end. But I’m only able to do this manually. For example if I hardcode the address with “per_page=20” it will show all 20. But the problem is I won’t always know how many to pull and if I try to pull 23 (1 more than there are) it just errors out and doesn’t load anything at all.
To show the code I currently use (it’s been shortened considerably to show the important parts):
[php]<?php
$packages = ‘http://[email protected]/packages.xml’;
$products = ‘http://[email protected]/products.xml’;
$orders = ‘http://[email protected]/orders.xml’;
$packagelist = new SimpleXMLElement($packages,null,true);
$productlist = new SimpleXMLElement($products,null,true);
$orderslist = new SimpleXMLElement($orders,null,true);
foreach($productlist->children() as $child) {
echo $child->getName().":
";
foreach($child->children() as $subchild) {
echo “
—>”.$subchild->getName().": “.$subchild.”
";
}
}
foreach($orderslist->children() as $child) {
echo $child->getName().":
";
foreach($child->children() as $subchild) {
echo “
—>”.$subchild->getName().": “.$subchild.”
";
}
}
?>[/php]
Along with this, I’m curious as to if my iteration method is efficient. As it is, I use the following (it’s set to read in the order list and compare the site’s email address and name to the list. If it hits, it takes the purchase ID number and parses the package and product lists to pull the names of the purchases):
[php]foreach($orderslist as $child) {
$purchaser = $child->{‘buyer-name’};
$email_addy = $child->{‘buyer-email’};
$download_url = $child->{‘download-url’};
$valid = $child->{‘valid-until’};
$expires = date(“F j, Y”, strtotime("$valid"));
$product_id = $child->{‘product-id’};
$package_id = $child->{‘package-id’};
if ($email_addy == “$activeEmail” && $purchaser == “$usersName” && ($child->state == “complete” || $child->state == “free”)){
foreach($productlist as $testing){
if ($product_id == “{$testing->id}”){
echo “
}
}[/php]
Lastly, a concern I have with the setup is that as the system gets more purchases, the list being read in will keep getting longer and longer. As it is, every time someone visits their account page it pulls in that list to get the most up-to-date information. Is there a more efficient way to handle this?
API Documentation: http://www.digitaldeliveryapp.com/developers/api/introduction
Thanks in advance! I’m very new to this, as you can see, but this has been a great learning experience so far, :).