twitter.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. require_once __DIR__.'/../../vendor/autoload.php';
  3. // Create server
  4. $server = new League\OAuth1\Client\Server\Twitter(array(
  5. 'identifier' => 'your-identifier',
  6. 'secret' => 'your-secret',
  7. 'callback_uri' => "http://your-callback-uri/",
  8. ));
  9. // Start session
  10. session_start();
  11. // Step 4
  12. if (isset($_GET['user'])) {
  13. // Check somebody hasn't manually entered this URL in,
  14. // by checking that we have the token credentials in
  15. // the session.
  16. if ( ! isset($_SESSION['token_credentials'])) {
  17. echo 'No token credentials.';
  18. exit(1);
  19. }
  20. // Retrieve our token credentials. From here, it's play time!
  21. $tokenCredentials = unserialize($_SESSION['token_credentials']);
  22. // // Below is an example of retrieving the identifier & secret
  23. // // (formally known as access token key & secret in earlier
  24. // // OAuth 1.0 specs).
  25. // $identifier = $tokenCredentials->getIdentifier();
  26. // $secret = $tokenCredentials->getSecret();
  27. // Some OAuth clients try to act as an API wrapper for
  28. // the server and it's API. We don't. This is what you
  29. // get - the ability to access basic information. If
  30. // you want to get fancy, you should be grabbing a
  31. // package for interacting with the APIs, by using
  32. // the identifier & secret that this package was
  33. // designed to retrieve for you. But, for fun,
  34. // here's basic user information.
  35. $user = $server->getUserDetails($tokenCredentials);
  36. var_dump($user);
  37. // Step 3
  38. } elseif (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
  39. // Retrieve the temporary credentials from step 2
  40. $temporaryCredentials = unserialize($_SESSION['temporary_credentials']);
  41. // Third and final part to OAuth 1.0 authentication is to retrieve token
  42. // credentials (formally known as access tokens in earlier OAuth 1.0
  43. // specs).
  44. $tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']);
  45. // Now, we'll store the token credentials and discard the temporary
  46. // ones - they're irrelevant at this stage.
  47. unset($_SESSION['temporary_credentials']);
  48. $_SESSION['token_credentials'] = serialize($tokenCredentials);
  49. session_write_close();
  50. // Redirect to the user page
  51. header("Location: http://{$_SERVER['HTTP_HOST']}/?user=user");
  52. exit;
  53. // Step 2.5 - denied request to authorize client
  54. } elseif (isset($_GET['denied'])) {
  55. echo 'Hey! You denied the client access to your Twitter account! If you did this by mistake, you should <a href="?go=go">try again</a>.';
  56. // Step 2
  57. } elseif (isset($_GET['go'])) {
  58. // First part of OAuth 1.0 authentication is retrieving temporary credentials.
  59. // These identify you as a client to the server.
  60. $temporaryCredentials = $server->getTemporaryCredentials();
  61. // Store the credentials in the session.
  62. $_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
  63. session_write_close();
  64. // Second part of OAuth 1.0 authentication is to redirect the
  65. // resource owner to the login screen on the server.
  66. $server->authorize($temporaryCredentials);
  67. // Step 1
  68. } else {
  69. // Display link to start process
  70. echo '<a href="?go=go">Login</a>';
  71. }