forked from retailcrm/api-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_customers_list.php
More file actions
47 lines (41 loc) · 1.57 KB
/
example_customers_list.php
File metadata and controls
47 lines (41 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
use RetailCrm\Api\Client;
use RetailCrm\Api\Enum\PaginationLimit;
use RetailCrm\Api\Factory\SimpleClientFactory;
use RetailCrm\Api\Model\Entity\Customers\CustomerPhone;
use RetailCrm\Api\Model\Request\Customers\CustomersRequest;
use RetailCrm\Api\Model\Response\Customers\CustomersResponse;
require __DIR__ . '/../../../../vendor/autoload.php';
require __DIR__ . '/PaginatedRequestIterator.php';
$client = SimpleClientFactory::createClient('https://test.retailcrm.pro/', 'apiKey');
$request = new CustomersRequest();
$request->limit = PaginationLimit::LIMIT_100;
$request->page = 1;
$paginator = (new PaginatedRequestIterator($client))
->setRequest($request)
->setNextPageFunc(static function (CustomersRequest $request, CustomersResponse $response, int $page) {
$request->page = $page;
})
->setExtractBatchFunc(static function (CustomersResponse $response) {
return $response->customers;
})
->setSendResponseFunc(static function (Client $client, CustomersRequest $request) {
return $client->customers->list($request);
});
/** @var \RetailCrm\Api\Model\Entity\Customers\Customer $customer */
foreach ($paginator as $customer) {
printf(
'%d: %s %s %s <%s> (%s) %s',
$customer->id,
$customer->firstName,
$customer->lastName,
$customer->patronymic,
implode(', ', array_map(static function (CustomerPhone $phone) {
return $phone->number;
}, $customer->phones)),
$customer->email, PHP_EOL
);
}
if ($paginator->isFetchFailed()) {
echo $paginator->getError();
}