Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
requestfacebook.php
1<?
2
4
7
9{
10 const TYPE_CODE = 'facebook';
11 const REST_METHOD_PREFIX = 'seo.client.ads.facebook';
12
13 protected function directQuery(array $params = array())
14 {
15 $url = 'https://graph.facebook.com/v8.0/';
16 $url .= $params['endpoint'];
17
18 $clientParameters = is_array($params['fields']) ? $params['fields'] : array();
19 $clientParameters = $clientParameters + array('access_token' => $this->adapter->getToken());
20
21 $result = '';
22 if ($params['method'] == 'GET')
23 {
24 $url .= '?' . http_build_query($clientParameters, "", "&");
25 $result = $this->client->get($url);
26 }
27 elseif ($params['method'] == 'DELETE')
28 {
29 $result = $this->client->delete($url, $clientParameters, true);
30 }
31 else
32 {
33 $result = $this->client->post($url, $clientParameters, true);
34 }
35 if (!$params['has_pagination'])
36 return $result;
37
38 try
39 {
40 $partialResult = $result;
41 $result = [];
42 $page = 1;
43 do
44 {
45 $decodedResult = Json::decode($partialResult);
46 $nextPage = ($decodedResult['paging'] && $decodedResult['paging']['next']) ? $decodedResult['paging']['next'] : false;
47 unset($decodedResult['paging']);
48
49 $result = array_merge_recursive($result, $decodedResult);
50
51 if ($nextPage)
52 {
53 $this->client->query($params['method'], $nextPage);
54 $partialResult = $this->client->getResult();
55 $page++;
56 }
57 else
58 {
59 if ($page == 1) // if haven't ['paging']['next'] in original response
60 {
61 return $partialResult;
62 }
63 break;
64 }
65 }
66 while($nextPage && $page < 20); // max 500 items
67
68 return Json::encode($result);
69 }
70 catch (\Exception $e)
71 {
72 return $result;
73 }
74 }
75}