|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Borfast\Socializr\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
use Borfast\Socializr\Exceptions\DuplicatePostException; |
|
6
|
|
|
use OAuth\Common\Storage\Exception\TokenNotFoundException; |
|
7
|
|
|
use Borfast\Socializr\Exceptions\AuthorizationException; |
|
8
|
|
|
use Borfast\Socializr\Exceptions\GenericPostingException; |
|
9
|
|
|
use Borfast\Socializr\Post; |
|
10
|
|
|
use Borfast\Socializr\Profile; |
|
11
|
|
|
use Borfast\Socializr\Response; |
|
12
|
|
|
use Borfast\Socializr\Exceptions\ExpiredTokenException; |
|
13
|
|
|
|
|
14
|
|
|
class Twitter extends AbstractConnector |
|
15
|
|
|
{ |
|
16
|
|
|
public static $provider = 'Twitter'; |
|
17
|
|
|
|
|
18
|
|
|
protected $user_id; |
|
19
|
|
|
protected $screen_name; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
public function request($path, $method = 'GET', $params = [], $headers = []) |
|
23
|
|
|
{ |
|
24
|
|
|
$result = parent::request($path, $method, $params, $headers); |
|
25
|
|
|
$json_result = json_decode($result, true); |
|
26
|
|
|
|
|
27
|
|
|
// Since Twitter can return more than one error, we'll need to check if |
|
28
|
|
|
// we have an expired token separately. |
|
29
|
|
|
$error_type = 'generic'; |
|
30
|
|
|
if (isset($json_result['errors'])) { |
|
31
|
|
|
$errors = $json_result['errors']; |
|
32
|
|
|
$msg = 'Error accessing Twitter. Error count: %s.'; |
|
33
|
|
|
$msg = sprintf($msg, count($errors)); |
|
34
|
|
|
$i = 0; |
|
35
|
|
|
|
|
36
|
|
|
foreach ($errors as $error) { |
|
37
|
|
|
$msg2 = "\nError %d -- Error code: %s. Message: %s"; |
|
38
|
|
|
$msg .= sprintf($msg2, $i, $error['code'], $error['message']); |
|
39
|
|
|
$i++; |
|
40
|
|
|
|
|
41
|
|
|
// If it's an expired token... |
|
42
|
|
|
if ($error['code'] == 89) { |
|
43
|
|
|
$error_type = 'expired'; |
|
44
|
|
|
} elseif ($error['code'] == 187) { |
|
45
|
|
|
$error_type = 'duplicate'; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($error_type == 'expired') { |
|
50
|
|
|
throw new ExpiredTokenException($msg); |
|
51
|
|
|
} elseif ($error_type == 'duplicate') { |
|
52
|
|
|
throw new DuplicatePostException($msg); |
|
53
|
|
|
} else { |
|
54
|
|
|
throw new GenericPostingException($msg); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function post(Post $post) |
|
63
|
|
|
{ |
|
64
|
|
|
$path = '/statuses/update.json'; |
|
65
|
|
|
$method = 'POST'; |
|
66
|
|
|
$params = [ |
|
67
|
|
|
'status' => $post->body |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
if (!empty($post->media)) { |
|
71
|
|
|
$params['media_ids'] = implode(',', $post->media); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$result = $this->request($path, $method, $params); |
|
75
|
|
|
|
|
76
|
|
|
$response = new Response; |
|
77
|
|
|
$response->setRawResponse(json_encode($result)); |
|
78
|
|
|
$result_json = json_decode($result); |
|
79
|
|
|
$response->setProvider('Twitter'); |
|
80
|
|
|
$response->setPostId($result_json->id_str); |
|
81
|
|
|
|
|
82
|
|
|
return $response; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
public function postMedia($media) |
|
87
|
|
|
{ |
|
88
|
|
|
$path = 'https://upload.twitter.com/1.1/media/upload.json'; |
|
89
|
|
|
$method = 'POST'; |
|
90
|
|
|
$params = [ |
|
91
|
|
|
'media' => $media |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$result = $this->request($path, $method, $params); |
|
95
|
|
|
$json_result = json_decode($result, true); |
|
96
|
|
|
|
|
97
|
|
|
return $json_result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Twitter needs an extra step for authentication before providing an |
|
103
|
|
|
* authorization URL. |
|
104
|
|
|
* |
|
105
|
|
|
* @author Raúl Santos |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getAuthorizationUri(array $params = []) |
|
108
|
|
|
{ |
|
109
|
|
|
$token = $this->service->requestRequestToken(); |
|
|
|
|
|
|
110
|
|
|
$extra = ['oauth_token' => $token->getRequestToken()]; |
|
111
|
|
|
return parent::getAuthorizationUri($extra); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Retrieve the auth token from the provider's response and store it. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function storeOauthToken($params) |
|
119
|
|
|
{ |
|
120
|
|
|
try { |
|
121
|
|
|
$token = $this->service->getStorage()->retrieveAccessToken('Twitter'); |
|
122
|
|
|
} catch (TokenNotFoundException $e) { |
|
123
|
|
|
throw new AuthorizationException(); |
|
124
|
|
|
} |
|
125
|
|
|
$result = $this->service->requestAccessToken($params['oauth_token'], $params['oauth_verifier'], $token->getRequestTokenSecret()); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$extra_params = $result->getExtraParams(); |
|
128
|
|
|
$this->user_id = $extra_params['user_id']; |
|
129
|
|
|
$this->screen_name = $extra_params['screen_name']; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
public function getProfile() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$path = '/account/verify_credentials.json?skip_status=1'; |
|
135
|
|
|
$result = $this->request($path); |
|
136
|
|
|
$profile_json = json_decode($result, true); |
|
137
|
|
|
|
|
138
|
|
|
$mapping = [ |
|
139
|
|
|
'id' => 'id_str', |
|
140
|
|
|
// 'email' => 'email', |
|
|
|
|
|
|
141
|
|
|
'name' => 'name', |
|
142
|
|
|
'first_name' => 'first_name', |
|
143
|
|
|
'middle_name' => 'middle_name', |
|
144
|
|
|
'last_name' => 'last_name', |
|
145
|
|
|
'username' => 'screen_name', |
|
146
|
|
|
'link' => 'link' |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
$profile = Profile::create($mapping, $profile_json); |
|
150
|
|
|
$profile->provider = static::$provider; |
|
151
|
|
|
$profile->raw_response = $result; |
|
152
|
|
|
$profile->link = 'https://twitter.com/'.$profile_json['screen_name']; |
|
153
|
|
|
|
|
154
|
|
|
return $profile; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function getPermissions() |
|
158
|
|
|
{ |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
View Code Duplication |
public function getStats() |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
$path = '/followers/ids.json?user_id='.$this->id; |
|
165
|
|
|
$response = $this->request($path); |
|
166
|
|
|
$response = json_decode($response); |
|
167
|
|
|
$response = count($response->ids); |
|
168
|
|
|
return $response; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.