1 | <?php |
---|
2 | /*! |
---|
3 | * HybridAuth |
---|
4 | * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth |
---|
5 | * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Hybrid_Providers_Twitter provider adapter based on OAuth1 protocol |
---|
10 | */ |
---|
11 | class Hybrid_Providers_Twitter extends Hybrid_Provider_Model_OAuth1 |
---|
12 | { |
---|
13 | /** |
---|
14 | * IDp wrappers initializer |
---|
15 | */ |
---|
16 | function initialize() |
---|
17 | { |
---|
18 | parent::initialize(); |
---|
19 | |
---|
20 | // Provider api end-points |
---|
21 | $this->api->api_base_url = "https://api.twitter.com/1/"; |
---|
22 | $this->api->authorize_url = "https://api.twitter.com/oauth/authenticate"; |
---|
23 | $this->api->request_token_url = "https://api.twitter.com/oauth/request_token"; |
---|
24 | $this->api->access_token_url = "https://api.twitter.com/oauth/access_token"; |
---|
25 | |
---|
26 | $this->api->curl_auth_header = false; |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * load the user profile from the IDp api client |
---|
31 | */ |
---|
32 | function getUserProfile() |
---|
33 | { |
---|
34 | $response = $this->api->get( 'account/verify_credentials.json' ); |
---|
35 | |
---|
36 | // check the last HTTP status code returned |
---|
37 | if ( $this->api->http_code != 200 ){ |
---|
38 | throw new Exception( "User profile request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 6 ); |
---|
39 | } |
---|
40 | |
---|
41 | if ( ! is_object( $response ) || ! isset( $response->id ) ){ |
---|
42 | throw new Exception( "User profile request failed! {$this->providerId} api returned an invalid response.", 6 ); |
---|
43 | } |
---|
44 | |
---|
45 | # store the user profile. |
---|
46 | $this->user->profile->identifier = (property_exists($response,'id'))?$response->id:""; |
---|
47 | $this->user->profile->displayName = (property_exists($response,'screen_name'))?$response->screen_name:""; |
---|
48 | $this->user->profile->description = (property_exists($response,'description'))?$response->description:""; |
---|
49 | $this->user->profile->firstName = (property_exists($response,'name'))?$response->name:""; |
---|
50 | $this->user->profile->photoURL = (property_exists($response,'profile_image_url'))?$response->profile_image_url:""; |
---|
51 | $this->user->profile->profileURL = (property_exists($response,'screen_name'))?("http://twitter.com/".$response->screen_name):""; |
---|
52 | $this->user->profile->webSiteURL = (property_exists($response,'url'))?$response->url:""; |
---|
53 | $this->user->profile->region = (property_exists($response,'location'))?$response->location:""; |
---|
54 | |
---|
55 | return $this->user->profile; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * load the user contacts |
---|
60 | */ |
---|
61 | function getUserContacts() |
---|
62 | { |
---|
63 | $parameters = array( 'cursor' => '-1' ); |
---|
64 | $response = $this->api->get( 'friends/ids.json', $parameters ); |
---|
65 | |
---|
66 | // check the last HTTP status code returned |
---|
67 | if ( $this->api->http_code != 200 ){ |
---|
68 | throw new Exception( "User contacts request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) ); |
---|
69 | } |
---|
70 | |
---|
71 | if( ! $response || ! count( $response->ids ) ){ |
---|
72 | return ARRAY(); |
---|
73 | } |
---|
74 | |
---|
75 | // 75 id per time should be okey |
---|
76 | $contactsids = array_chunk ( $response->ids, 75 ); |
---|
77 | |
---|
78 | $contacts = ARRAY(); |
---|
79 | |
---|
80 | foreach( $contactsids as $chunk ){ |
---|
81 | $parameters = array( 'user_id' => implode( ",", $chunk ) ); |
---|
82 | $response = $this->api->get( 'users/lookup.json', $parameters ); |
---|
83 | |
---|
84 | // check the last HTTP status code returned |
---|
85 | if ( $this->api->http_code != 200 ){ |
---|
86 | throw new Exception( "User contacts request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) ); |
---|
87 | } |
---|
88 | |
---|
89 | if( $response && count( $response ) ){ |
---|
90 | foreach( $response as $item ){ |
---|
91 | $uc = new Hybrid_User_Contact(); |
---|
92 | |
---|
93 | $uc->identifier = (property_exists($item,'id'))?$item->id:""; |
---|
94 | $uc->displayName = (property_exists($item,'name'))?$item->name:""; |
---|
95 | $uc->profileURL = (property_exists($item,'screen_name'))?("http://twitter.com/".$item->screen_name):""; |
---|
96 | $uc->photoURL = (property_exists($item,'profile_image_url'))?$item->profile_image_url:""; |
---|
97 | $uc->description = (property_exists($item,'description'))?$item->description:""; |
---|
98 | |
---|
99 | $contacts[] = $uc; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | return $contacts; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * update user status |
---|
109 | */ |
---|
110 | function setUserStatus( $status ) |
---|
111 | { |
---|
112 | $parameters = array( 'status' => $status ); |
---|
113 | $response = $this->api->post( 'statuses/update.json', $parameters ); |
---|
114 | |
---|
115 | // check the last HTTP status code returned |
---|
116 | if ( $this->api->http_code != 200 ){ |
---|
117 | throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) ); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * load the user latest activity |
---|
123 | * - timeline : all the stream |
---|
124 | * - me : the user activity only |
---|
125 | * |
---|
126 | * by default return the timeline |
---|
127 | */ |
---|
128 | function getUserActivity( $stream ) |
---|
129 | { |
---|
130 | if( $stream == "me" ){ |
---|
131 | $response = $this->api->get( 'statuses/user_timeline.json' ); |
---|
132 | } |
---|
133 | else{ |
---|
134 | $response = $this->api->get( 'statuses/home_timeline.json' ); |
---|
135 | } |
---|
136 | |
---|
137 | // check the last HTTP status code returned |
---|
138 | if ( $this->api->http_code != 200 ){ |
---|
139 | throw new Exception( "User activity stream request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) ); |
---|
140 | } |
---|
141 | |
---|
142 | if( ! $response ){ |
---|
143 | return ARRAY(); |
---|
144 | } |
---|
145 | |
---|
146 | $activities = ARRAY(); |
---|
147 | |
---|
148 | foreach( $response as $item ){ |
---|
149 | $ua = new Hybrid_User_Activity(); |
---|
150 | |
---|
151 | $ua->id = (property_exists($item,'id'))?$item->id:""; |
---|
152 | $ua->date = (property_exists($item,'created_at'))?strtotime($item->created_at):""; |
---|
153 | $ua->text = (property_exists($item,'text'))?$item->text:""; |
---|
154 | |
---|
155 | $ua->user->identifier = (property_exists($item->user,'id'))?$item->user->id:""; |
---|
156 | $ua->user->displayName = (property_exists($item->user,'name'))?$item->user->name:""; |
---|
157 | $ua->user->profileURL = (property_exists($item->user,'screen_name'))?("http://twitter.com/".$item->user->screen_name):""; |
---|
158 | $ua->user->photoURL = (property_exists($item->user,'profile_image_url'))?$item->user->profile_image_url:""; |
---|
159 | |
---|
160 | $activities[] = $ua; |
---|
161 | } |
---|
162 | |
---|
163 | return $activities; |
---|
164 | } |
---|
165 | } |
---|