1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/craft/facebook/ |
4
|
|
|
* @copyright Copyright (c) 2018, Dukt |
5
|
|
|
* @license https://dukt.net/craft/facebook/docs/license |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\facebook\services; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\Exception\RequestException; |
13
|
|
|
use yii\base\Component; |
14
|
|
|
use dukt\facebook\Plugin as Facebook; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Api service |
18
|
|
|
* |
19
|
|
|
* @author Dukt <[email protected]> |
20
|
|
|
* @since 2.0 |
21
|
|
|
*/ |
22
|
|
|
class Api extends Component |
23
|
|
|
{ |
24
|
|
|
// Properties |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $baseApiUrl = 'https://graph.facebook.com/'; |
31
|
|
|
|
32
|
|
|
// Public Methods |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Performs an authenticated GET request on Facebook’s API |
37
|
|
|
* |
38
|
|
|
* @param null $uri |
|
|
|
|
39
|
|
|
* @param null $query |
|
|
|
|
40
|
|
|
* @param null $headers |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
44
|
|
|
*/ |
45
|
|
|
public function get($uri = null, $query = null, $headers = null) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$client = $this->getClient(); |
49
|
|
|
|
50
|
|
|
$options['query'] = ($query ? $query : []); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if ($headers) { |
53
|
|
|
$options['headers'] = $headers; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$response = $client->request('GET', $uri, $options); |
57
|
|
|
|
58
|
|
|
return json_decode($response->getBody(), true); |
59
|
|
|
} catch (RequestException $e) { |
60
|
|
|
Craft::error('Error requesting Facebook’s API: '.$e->getResponse()->getBody(), __METHOD__); |
61
|
|
|
throw $e; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $facebookInsightsObjectId |
67
|
|
|
* @param array $options |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
71
|
|
|
*/ |
72
|
|
|
public function getInsights($facebookInsightsObjectId, array $options = []) |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
$pageAccessTokenResponse = $this->get('/'.$facebookInsightsObjectId, ['fields' => 'access_token']); |
76
|
|
|
|
77
|
|
|
if(empty($pageAccessTokenResponse['access_token'])) { |
78
|
|
|
throw new \Exception('Couldn’t retrieve page access token for '.$facebookInsightsObjectId); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$client = $this->getClient($pageAccessTokenResponse['access_token']); |
82
|
|
|
|
83
|
|
|
$response = $client->request('GET', '/'.$facebookInsightsObjectId.'/insights', $options); |
84
|
|
|
|
85
|
|
|
return json_decode($response->getBody(), true); |
86
|
|
|
} catch (RequestException $e) { |
87
|
|
|
Craft::error('Error requesting insights: '.$e->getResponse()->getBody(), __METHOD__); |
88
|
|
|
throw $e; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Private Methods |
93
|
|
|
// ========================================================================= |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the Facebook API URL. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
private function getApiUrl() |
101
|
|
|
{ |
102
|
|
|
$apiVersion = Facebook::$plugin->getSettings()->apiVersion; |
103
|
|
|
|
104
|
|
|
return $this->baseApiUrl.$apiVersion.'/'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the authenticated Facebook OAuth client. |
109
|
|
|
* |
110
|
|
|
* @param null $accessToken |
|
|
|
|
111
|
|
|
* |
112
|
|
|
* @return Client |
113
|
|
|
*/ |
114
|
|
|
private function getClient($accessToken = null): Client |
115
|
|
|
{ |
116
|
|
|
if(!$accessToken) { |
117
|
|
|
$token = Facebook::$plugin->getOauth()->getToken(); |
118
|
|
|
$accessToken = $token->getToken(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$headers = []; |
122
|
|
|
|
123
|
|
|
if ($accessToken) { |
124
|
|
|
$headers['Authorization'] = 'Bearer '.$accessToken; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$options = [ |
128
|
|
|
'base_uri' => $this->getApiUrl(), |
129
|
|
|
'headers' => $headers |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
return new Client($options); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|