|
1 | 1 | <?php |
2 | | -// require twitterOAuth lib |
3 | | -require_once('twitteroauth/twitterOAuth.php'); |
4 | | - |
5 | | -/* Sessions are used to keep track of tokens while user authenticates with twitter */ |
6 | | -session_start(); |
7 | | -/* Consumer key from twitter */ |
8 | | -$consumer_key = ''; |
9 | | -/* Consumer Secret from twitter */ |
10 | | -$consumer_secret = ''; |
11 | | -/* Set up placeholder */ |
12 | | -$content = NULL; |
13 | | -/* Set state if previous session */ |
14 | | -$state = $_SESSION['oauth_state']; |
15 | | -/* Checks if oauth_token is set from returning from twitter */ |
16 | | -$session_token = $_SESSION['oauth_request_token']; |
17 | | -/* Checks if oauth_token is set from returning from twitter */ |
18 | | -$oauth_token = $_REQUEST['oauth_token']; |
19 | | -/* Set section var */ |
20 | | -$section = $_REQUEST['section']; |
21 | | - |
22 | | -/* Clear PHP sessions */ |
23 | | -if ($_REQUEST['test'] === 'clear') {/*{{{*/ |
24 | | - session_destroy(); |
25 | | - session_start(); |
26 | | -}/*}}}*/ |
27 | | - |
28 | | -/* If oauth_token is missing get it */ |
29 | | -if ($_REQUEST['oauth_token'] != NULL && $_SESSION['oauth_state'] === 'start') {/*{{{*/ |
30 | | - $_SESSION['oauth_state'] = $state = 'returned'; |
31 | | -}/*}}}*/ |
32 | | - |
33 | | -/* |
34 | | - * Switch based on where in the process you are |
35 | | - * |
36 | | - * 'default': Get a request token from twitter for new user |
37 | | - * 'returned': The user has authorize the app on twitter |
| 2 | +/** |
| 3 | + * @file |
| 4 | + * User has successfully authenticated with Twitter. Access tokens saved to session and DB. |
38 | 5 | */ |
39 | | -switch ($state) {/*{{{*/ |
40 | | - default: |
41 | | - /* Create TwitterOAuth object with app key/secret */ |
42 | | - $to = new TwitterOAuth($consumer_key, $consumer_secret); |
43 | | - /* Request tokens from twitter */ |
44 | | - $tok = $to->getRequestToken(); |
45 | | - |
46 | | - /* Save tokens for later */ |
47 | | - $_SESSION['oauth_request_token'] = $token = $tok['oauth_token']; |
48 | | - $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret']; |
49 | | - $_SESSION['oauth_state'] = "start"; |
50 | | - |
51 | | - /* Build the authorization URL */ |
52 | | - $request_link = $to->getAuthorizeURL($token); |
53 | 6 |
|
54 | | - /* Build link that gets user to twitter to authorize the app */ |
55 | | - $content = 'Click on the link to go to twitter to authorize your account.'; |
56 | | - $content .= '<br /><a href="'.$request_link.'">'.$request_link.'</a>'; |
57 | | - break; |
58 | | - case 'returned': |
59 | | - /* If the access tokens are already set skip to the API call */ |
60 | | - if ($_SESSION['oauth_access_token'] === NULL && $_SESSION['oauth_access_token_secret'] === NULL) { |
61 | | - /* Create TwitterOAuth object with app key/secret and token key/secret from default phase */ |
62 | | - $to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']); |
63 | | - /* Request access tokens from twitter */ |
64 | | - $tok = $to->getAccessToken(); |
65 | | - |
66 | | - /* Save the access tokens. Normally these would be saved in a database for future use. */ |
67 | | - $_SESSION['oauth_access_token'] = $tok['oauth_token']; |
68 | | - $_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret']; |
69 | | - } |
70 | | - /* Random copy */ |
71 | | - $content = 'your account should now be registered with twitter. Check here:<br />'; |
72 | | - $content .= '<a href="https://twitter.com/account/connections">https://twitter.com/account/connections</a>'; |
73 | | - |
74 | | - /* Create TwitterOAuth with app key/secret and user access key/secret */ |
75 | | - $to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']); |
76 | | - /* Run request on twitter API as user. */ |
77 | | - $content = $to->OAuthRequest('https://twitter.com/account/verify_credentials.xml', array(), 'GET'); |
78 | | - //$content = $to->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => 'Test OAuth update. #testoauth'), 'POST'); |
79 | | - //$content = $to->OAuthRequest('https://twitter.com/statuses/replies.xml', array(), 'POST'); |
80 | | - break; |
81 | | -}/*}}}*/ |
82 | | -?> |
83 | | - |
84 | | -<html> |
85 | | - <head> |
86 | | - <title>Twitter OAuth in PHP</title> |
87 | | - </head> |
88 | | - <body> |
89 | | - <h2>Welcome to a Twitter OAuth PHP example.</h2> |
90 | | - <p>This site is a basic showcase of Twitters new OAuth authentication method. Everything is saved in sessions. If you want to start over <a href='<?php echo $_SERVER['PHP_SELF']; ?>?test=clear'>clear sessions</a>.</p> |
91 | | - |
92 | | - <p> |
93 | | - Get the code powering this at <a href='http://github.com/abraham/twitteroauth'>http://github.com/abraham/twitteroauth</a> |
94 | | - <br /> |
95 | | - Read the documentation at <a href='https://docs.google.com/View?docID=dcf2dzzs_2339fzbfsf4'>https://docs.google.com/View?docID=dcf2dzzs_2339fzbfsf4</a> |
96 | | - </p> |
97 | | - |
98 | | - <p><pre><?php print_r($content); ?><pre></p> |
99 | | - |
100 | | - </body> |
101 | | -</html> |
| 7 | +/* Load required lib files. */ |
| 8 | +session_start(); |
| 9 | +require_once('twitteroauth/twitteroauth.php'); |
| 10 | +require_once('config.php'); |
| 11 | + |
| 12 | +/* Get user access tokens out of the session. */ |
| 13 | +$access_token = $_SESSION['access_token']; |
| 14 | +/* If access tokens are not available redirect to connect page. */ |
| 15 | +if (empty($access_token['oauth_token']) || empty($access_token['oauth_token_secret'])) { |
| 16 | + header('Location: '.$home_page.'/clearsessions.php'); |
| 17 | +} |
| 18 | + |
| 19 | +/* Create a TwitterOauth object with consumer/user tokens. */ |
| 20 | +$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); |
| 21 | + |
| 22 | +/* If method is set change API call made. Test is called by default. */ |
| 23 | +$content = '<pre>'.$connection->get('account/verify_credentials').'</pre>'; |
| 24 | + |
| 25 | +/* Some example calls */ |
| 26 | +//$connection->get('users/show', array('screen_name' => 'abraham'))); |
| 27 | +//$connection->post('statuses/update', array('status' => date(DATE_RFC822))); |
| 28 | +//$connection->post('statuses/destroy', array('id' => 5437877770)); |
| 29 | +//$connection->post('friendships/create', array('id' => 9436992))); |
| 30 | +//$connection->post('friendships/destroy', array('id' => 9436992))); |
| 31 | + |
| 32 | +/* Include HTML to display on the page */ |
| 33 | +include('html.inc'); |
0 commit comments