forked from EmanueleMinotto/Gravatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
167 lines (142 loc) · 4.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace EmanueleMinotto\Gravatar;
use GuzzleHttp\Client as GuzzleHttp_Client;
use GuzzleHttp\ClientInterface as GuzzleHttp_ClientInterface;
use GuzzleHttp\Exception\ClientException as GuzzleHttp_ClientException;
/**
* PHP library for gravatar.com.
*
* @author Emanuele Minotto <minottoemanuele@gmail.com>
*
* @link http://www.gravatar.com/
*/
class Client
{
/**
* Guzzle HTTP client.
*
* @var GuzzleHttp_Client
*/
private $httpClient;
/**
* Class constructor, with an alternative Guzzle HTTP client (optional).
*
* @param GuzzleHttp_ClientInterface|null $httpClient Guzzle 4/5 client.
*/
public function __construct(GuzzleHttp_ClientInterface $httpClient = null)
{
$this->setHttpClient($httpClient ?: new GuzzleHttp_Client());
}
/**
* HTTP client setter.
*
* @param GuzzleHttp_ClientInterface $httpClient Guzzle 4/5 client.
*
* @return self
*/
public function setHttpClient(GuzzleHttp_ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
return $this;
}
/**
* HTTP client getter.
*
* @return \GuzzleHttp\ClientInterface
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* Get user profile on gravatar.com.
*
* @param string $email User email.
* @param string $format Request format (default json).
* @param array $options Request options.
*
* @return string
*/
public function getProfileUrl($email, $format = 'json', array $options = [])
{
$url = 'https://www.gravatar.com/';
$url .= md5(strtolower(trim($email))).'.';
$url .= in_array($format, ['json', 'xml', 'php', 'vcf', 'qr']) ? $format : 'json';
if ($options) {
$url .= '?'.http_build_query($options);
}
return $url;
}
/**
* Get user profile data.
*
* @param string $email User email.
*
* @return array
*/
public function getProfile($email)
{
$url = $this->getProfileUrl($email);
$httpClient = &$this->httpClient;
$response = $httpClient->get($url);
$data = $response->json();
return $data['entry'][0];
}
/**
* Get user avatar image URL.
*
* @param string $email User email.
* @param int $size Image size (default 80).
* @param string $extension Image extension (default jpg).
* @param int $default Error response (default 404).
* @param string $rating Image rating (default G).
*
* @return string
*/
public function getAvatarUrl($email, $size = 80, $extension = 'jpg', $default = 404, $rating = 'g')
{
$url = 'https://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email))).'.'.$extension;
$url .= '?'.http_build_query([
'd' => $default,
'r' => $rating,
's' => $size,
]);
return $url;
}
/**
* Get user avatar image (as base 64 URI).
*
* @param string $email User email.
* @param int $size Image size (default 80).
* @param string $extension Image extension (default jpg).
* @param int $default Error response (default 404).
* @param string $rating Image rating (default G).
*
* @return string
*/
public function getAvatar($email, $size = 80, $extension = 'jpg', $default = 404, $rating = 'g')
{
$url = $this->getAvatarUrl($email, $size, $extension, $default, $rating);
$response = $this->httpClient->get($url);
$content = (string) $response->getBody();
$mimeType = $response->getHeader('content-type');
return 'data:'.$mimeType.';base64,'.base64_encode($content);
}
/**
* Used to check if a gravatar exists.
*
* @param string $email User email.
*
* @return bool
*/
public function exists($email)
{
try {
$this->getProfile($email);
return true;
} catch (GuzzleHttp_ClientException $exception) {
return false;
}
}
}