views:

161

answers:

2

Im using the Twitter OAuth class to connect to Twitter found here:

Currently the script just uses the tokens supplied but doesnt store them in a database, i'd like the script to do this.

This is what I currently have in my callback script:

<?php
/**
 * @file
 * Take the user when they return from Twitter. Get access tokens.
 * Verify credentials and redirect to based on response from Twitter.
 */

/* Start session and load lib */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

/* If the oauth_token is old redirect to the connect page. */
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
  $_SESSION['oauth_status'] = 'oldtoken';
  header('Location: ./clearsessions.php');
}

/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

/* Request access tokens from twitter */
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

/* Save the access tokens. Normally these would be saved in a database for future use. */
$_SESSION['access_token'] = $access_token;

/* Remove no longer needed request tokens */
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);

/* If HTTP response is 200 continue otherwise send to connect page to retry */
if (200 == $connection->http_code) {
  /* The user has been verified and the access tokens can be saved for future use */
  $_SESSION['status'] = 'verified';
  header('Location: ./index.php');
} else {
  /* Save HTTP status for error dialog on connnect page.*/
  header('Location: ./clearsessions.php');
}

How can I save the tokens to MySql, in what part of the script do I get the tokens?

A: 

The data you need is stored in the $_SESSION['access_token'] = $access_token; variable.

Try print_r($access_token);

Inside that varible you will find:

screen_name
user_id
oauth_token
oauth_token_secret

Which can be used for your app and stored in your database.

Jayrox
A: 

hey, this is may be a silly question which just killing me. How do I get to know that I'm gonna get $_SESSION['access_token'], $_SESSION['access_token']['oauth_token'], $_SESSION['access_token']['oauth_token_secret'] which found in Abraham's example. I mean, is it documented somewhere? if yes, can you please share the link?

crank