LKDevelopment / hetzner-cloud-php-sdk

A PHP SDK for the Hetzner Cloud API
MIT License
107 stars 52 forks source link

I get information from 25 servers only #28

Closed dacrhu closed 4 years ago

dacrhu commented 4 years ago

Hi,

I have 41 servers in cloud. If I try get informations with $hetznerClient->servers()->all() method then get only 25. Can you help me?

Thank you!

LKaemmerling commented 4 years ago

All just respond with the first page of the servers. You need to change the size of the page (max: 50 servers per page) like:

$hetznerClient->servers()->all(new \LKDev\HetznerCloud\RequestOpts(50))

or if you want to get from the next page you need to change the page:

$hetznerClient->servers()->all(new \LKDev\HetznerCloud\RequestOpts(50,2))

I guess the best way to achieve to get all servers would be:


<?php
require_once __DIR__ . '/vendor/autoload.php';

$client = new \LKDev\HetznerCloud\HetznerAPIClient("<YOUR_API_TOKEN>");

$servers = [];
for ($i = 1; $i < PHP_INT_MAX; $i++) {
    $_s = $client->servers()->all(new \LKDev\HetznerCloud\RequestOpts(50, $i));
    $servers = array_merge($servers, $_s);
    if(empty($_s)){
        break;
    }
}

echo "Servers: ". count($servers).PHP_EOL;
LKaemmerling commented 4 years ago

But you are correct, the method is not correctly named. i will think about a better solution.

dacrhu commented 4 years ago
    $hetznerClient = new \LKDev\HetznerCloud\HetznerAPIClient($token);
    foreach ($hetznerClient->servers()->all(new \LKDev\HetznerCloud\RequestOpts(50)) as $server) {
        echo $project . ',' . $server->id . ',' . $server->name . ',' . $server->status . PHP_EOL;
    }

php ./get.php | wc -l

25

But your second solution working fine. Thank you!

LKaemmerling commented 4 years ago

@dacrhu as of 1.8.2 the first solution should work too. There was a wrong parameter assigned within the request, so the per_page value did not work. (#29)