Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用版本min max-VendHQ分页_Php_Loops_Pagination - Fatal编程技术网

Php 如何使用版本min max-VendHQ分页

Php 如何使用版本min max-VendHQ分页,php,loops,pagination,Php,Loops,Pagination,我想对VendHQ API进行正确分页。有没有一个例子可以让我看看min/max版本是如何工作的 我确信这是一个简单的PHP循环,我已经尝试了一些方法,但我不知道最好的做法。如果能看到一段代码片段,说明如何正确地构造它来查询Vend,那就太好了 我希望输出将是Vend的结果,我可以将其放入自己的数组中进行操作。让我们试试。我认为您已经有了http客户端,如果没有的话,您应该看看或类似的东西 每个响应中都有一个max参数,您还可以在查询中设置after参数来控制当前页面。不要测试,只是一个例子,所

我想对VendHQ API进行正确分页。有没有一个例子可以让我看看min/max版本是如何工作的

我确信这是一个简单的PHP循环,我已经尝试了一些方法,但我不知道最好的做法。如果能看到一段代码片段,说明如何正确地构造它来查询Vend,那就太好了


我希望输出将是Vend的结果,我可以将其放入自己的数组中进行操作。

让我们试试。我认为您已经有了http客户端,如果没有的话,您应该看看或类似的东西

每个响应中都有一个max参数,您还可以在查询中设置after参数来控制当前页面。不要测试,只是一个例子,所以要小心:

//initializing guzzle
$guzzle = new GuzzleHttp\Client(['base_uri' => 'http://api.api/api']);
//current max number
$after = 0;
//array for all data
$allData = [];
//loop while collection in response doesn't empty
do {
    //querying current data piece
    $response = $guzzle->request(
        'GET', 
        '/api/2.0/products', 
        ['query' => ['after' => $after]]
    );
    //decoding response
    $responseBody = (string) $response->getBody();
    $decodedResponse = json_decode($responseBody, true);
    //here you can do some operations with $decodedResponse['data']
    //you can store all data to array but this is bad, you can catch 
    //Allowed memory size of ... bytes exhausted
    $allData = array_merge($allData, $decodedResponse['data']);
    //recording current max to $after
    $after = $decodedResponse['version']['max'];
} while (!empty($decodedResponse['data']));

已经有一段时间了,但我想回到这个问题上-@marv255它工作得很好,在下面添加了我的最终代码,以帮助可能需要完整答案的其他人


你想迭代循环中的所有数据还是创建一个html寻呼机,如1 2 3。。。123456?Hi@marv255-只想循环所有结果并存储在一个数组中:如果您查看链接,我可以执行API 0.x版本-只需循环页面数并获得所有结果。但是从来没有用min/max等编写过API 2.0。不知道如何为此编写循环?太棒了!谢谢@marv255,我们一定会尝试一下!
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$sub_domain = 'vend-sub-domain';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$client = new \GuzzleHttp\Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://' . $sub_domain . '.vendhq.com/api/2.0/',
]);   


// Current max number
$after = 0;

//array for all data
$allData = [];

//loop while collection in response doesn't empty
do {
    //querying current data piece
    $response = $client->request(
        'GET',
        'products',
        [
          'headers'  => [
            'Authorization' => 'Bearer ' . $token
          ],
          'query' => ['after' => $after]
        ]
    );

    //decoding response
    $responseBody = (string) $response->getBody();
    $decodedResponse = json_decode($responseBody, true);
    //here you can do some operations with $decodedResponse['data']
    //you can store all data to array but this is bad, you can catch
    //Allowed memory size of ... bytes exhausted
    $allData = array_merge($allData, $decodedResponse['data']);
    //recording current max to $after
    $after = $decodedResponse['version']['max'];

} while (!empty($decodedResponse['data']));


echo '<pre>';
print_r($allData);
echo '</pre>';