-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
91 lines (75 loc) · 1.82 KB
/
Client.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace YaMarket;
use GuzzleHttp\Client as GC;
/**
* Class Client
* @package YaMarket
*/
class Client
{
private $key;
private $id;
# API settings
const api_base_url = 'https://api.content.market.yandex.ru/';
const API_VERSION = 'v2.1.0';
/**
* Client constructor.
* @param $key
* @param $id
*/
public function __construct($key, $id)
{
$this->key = $key;
$this->id = $id;
}
/**
* sets url for reviews
* @return string
*/
private function api_url()
{
return self::api_base_url . self::API_VERSION . "/models/{$this->id}/opinions";
}
/**
* @param int $page
* @return string
*/
private function url_params($page)
{
$fields = ['count' => 10, 'page' => $page, 'how' => 'DESC', 'sort' => 'DATE'];
$params = '';
foreach ($fields as $key => $field){
$params .= $key . '=' . $field . '&';
}
return $params ? self::api_url() . '?' . substr($params, 0, -1) : self::api_url();
}
/**
* @param int $page
* @return mixed
*/
public function get_reviews($page = 1)
{
$url = self::url_params($page);
$client = new GC();
$response = $client->request('GET', $url, ['headers' => ['Authorization' => $this->key]]);
$data = current(self::JsonInAr($response->getBody()))->opinions;
return $data;
}
/**
* terns json returned from api in arr
*
* @param $json
* @return mixed
*/
public static function JsonInAr($json)
{
$data = json_decode($json);
$error = json_last_error();
if ($error == JSON_ERROR_NONE)
{
return [$data, True];
}else{
return [json_last_error_msg(), False];
}
}
}