1 | <?php |
---|
2 | session_start(); |
---|
3 | |
---|
4 | // config and includes |
---|
5 | $config = dirname(__FILE__) . '/../../hybridauth/config.php'; |
---|
6 | require_once( "../../hybridauth/Hybrid/Auth.php" ); |
---|
7 | |
---|
8 | try{ |
---|
9 | // hybridauth EP |
---|
10 | $hybridauth = new Hybrid_Auth( $config ); |
---|
11 | |
---|
12 | // automatically try to login with Twitter |
---|
13 | $twitter = $hybridauth->authenticate( "Twitter" ); |
---|
14 | |
---|
15 | // return TRUE or False <= generally will be used to check if the user is connected to twitter before getting user profile, posting stuffs, etc.. |
---|
16 | $is_user_logged_in = $twitter->isUserConnected(); |
---|
17 | |
---|
18 | // get the user profile |
---|
19 | $user_profile = $twitter->getUserProfile(); |
---|
20 | |
---|
21 | // access user profile data |
---|
22 | echo "Ohai there! U are connected with: <b>{$twitter->id}</b><br />"; |
---|
23 | echo "As: <b>{$user_profile->displayName}</b><br />"; |
---|
24 | echo "And your provider user identifier is: <b>{$user_profile->identifier}</b><br />"; |
---|
25 | |
---|
26 | // or even inspect it |
---|
27 | echo "<pre>" . print_r( $user_profile, true ) . "</pre><br />"; |
---|
28 | |
---|
29 | // uncomment the line below to get user friends list |
---|
30 | // $twitter->getUserContacts(); |
---|
31 | |
---|
32 | // uncomment the line below to post something to twitter if you want to |
---|
33 | // $twitter->setUserStatus( "Hello world!" ); |
---|
34 | |
---|
35 | // ex. on how to access the twitter api with hybridauth |
---|
36 | // Returns the current count of friends, followers, updates (statuses) and favorites of the authenticating user. |
---|
37 | // https://dev.twitter.com/docs/api/1/get/account/totals |
---|
38 | $account_totals = $twitter->api()->get( 'account/totals.json' ); |
---|
39 | |
---|
40 | // print recived stats |
---|
41 | echo "Here some of yours stats on Twitter:<br /><pre>" . print_r( $account_totals, true ) . "</pre>"; |
---|
42 | |
---|
43 | // logout |
---|
44 | echo "Logging out.."; |
---|
45 | $twitter->logout(); |
---|
46 | } |
---|
47 | catch( Exception $e ){ |
---|
48 | // In case we have errors 6 or 7, then we have to use Hybrid_Provider_Adapter::logout() to |
---|
49 | // let hybridauth forget all about the user so we can try to authenticate again. |
---|
50 | |
---|
51 | // Display the recived error, |
---|
52 | // to know more please refer to Exceptions handling section on the userguide |
---|
53 | switch( $e->getCode() ){ |
---|
54 | case 0 : echo "Unspecified error."; break; |
---|
55 | case 1 : echo "Hybridauth configuration error."; break; |
---|
56 | case 2 : echo "Provider not properly configured."; break; |
---|
57 | case 3 : echo "Unknown or disabled provider."; break; |
---|
58 | case 4 : echo "Missing provider application credentials."; break; |
---|
59 | case 5 : echo "Authentication failed. " |
---|
60 | . "The user has canceled the authentication or the provider refused the connection."; |
---|
61 | break; |
---|
62 | case 6 : echo "User profile request failed. Most likely the user is not connected " |
---|
63 | . "to the provider and he should to authenticate again."; |
---|
64 | $twitter->logout(); |
---|
65 | break; |
---|
66 | case 7 : echo "User not connected to the provider."; |
---|
67 | $twitter->logout(); |
---|
68 | break; |
---|
69 | case 8 : echo "Provider does not support this feature."; break; |
---|
70 | } |
---|
71 | |
---|
72 | // well, basically your should not display this to the end user, just give him a hint and move on.. |
---|
73 | echo "<br /><br /><b>Original error message:</b> " . $e->getMessage(); |
---|
74 | |
---|
75 | echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>"; |
---|
76 | |
---|
77 | /* |
---|
78 | // If you want to get the previous exception - PHP 5.3.0+ |
---|
79 | // http://www.php.net/manual/en/language.exceptions.extending.php |
---|
80 | if ( $e->getPrevious() ) { |
---|
81 | echo "<h4>Previous exception</h4> " . $e->getPrevious()->getMessage() . "<pre>" . $e->getPrevious()->getTraceAsString() . "</pre>"; |
---|
82 | } |
---|
83 | */ |
---|
84 | } |
---|