Add values of this api

A

Anonymous

Guest
Hi everyone, I need to add the values of all addresses displayed in this api below. In the total of this example I have 2,484 addresses. The API only lets me display a maximum of 200 per page

How to display total values considering all addresses

Code:
<?php
$curl = curl_init();
curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.trongrid.io/v1/contracts/TFczxzPhnThNSqr5by8tvxsdCFRRz6cPNq/tokens?only_confirmed=true&only_unconfirmed=true&order_by=balance,desc&limit=200",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => [
    "Accept: application/json"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Could you help me to run this sum even with a page limitation?
 
Since it sounds like the API wants a limit of 200, work with it; otherwise you run the risk of getting kicked out.

Does the API already supply the information that you want?

Limits are put there for a reason - spammers, people that don't know what they're doing and plain old system resources getting bogged down by large requests.
 
An API lets you display 200 results at a time, but lets you page forward.

Adding just 200 would not have the correct result.
 
To calculate the total values considering all addresses, you need to make multiple API requests to fetch all the addresses and their respective values. Here's an example of how you can achieve this using a loop:
<?php
$curl = curl_init();
$totalValue = 0;

$url = "https://api.trongrid.io/v1/contract...onfirmed=true&order_by=balance,desc&limit=200";
$hasNextPage = true;

while ($hasNextPage) {
curl_setopt_array($curl, [
CURLOPT_URL => $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 => [
"Accept: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

if ($err) {
echo "cURL Error #:" . $err;
break;
} else {
$data = json_decode($response, true);
$addresses = $data['data'];

foreach ($addresses as $address) {
$totalValue += $address['balance'];
}

$hasNextPage = $data['meta']['page'] < $data['meta']['page_total'];
if ($hasNextPage) {
$url = $data['meta']['next_page_url'];
}
}
}

curl_close($curl);

echo "Total Value: " . $totalValue;
?>

In this code, we initialize a variable $totalValue to keep track of the cumulative value. Then, we use a while loop to make API requests for each page of addresses until there are no more pages left ($hasNextPage becomes false).

Inside the loop, we extract the addresses from the API response and iterate over each address to add its balance to the $totalValue variable. We also check if there is a next page available and update the URL accordingly.

Finally, outside the loop, we close the cURL connection and display the total value by echoing $totalValue.
 
Back
Top