I am working on a PHP project where I want to be able to post things automatically to twitter.
From my PHP program I have authorised my twitter app and stored the oauth_token and verifier in a mysql database.
When the user submits a form, it is supposed to post the data to twitter. To do this I am retrieving the oauth_token from the database to post the message but when I look at the response before the section of code that does actual status update I get an error saying
‘invalid/expired token’
Below is the code I am using
function postToTwitter($twitterMsg)
{
require ("../../social/phpHandler/twitterLib/EpiCurl.php");
require ("../../social/phpHandler/twitterLib/EpiOAuth.php");
require ("../../social/phpHandler/twitterLib/EpiTwitter.php");
require ("../../social/phpHandler/twitterLib/secret.php");
$query = "SELECT * FROM social_sites";
$result = mysql_query($query);
if ($result)
{
//session_start();
//$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
while ($myrow = mysql_fetch_array($result))
{
$oauth_token = $myrow['token'];
$verifier = $myrow['verifier'];
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$twitterObj->setToken($oauth_token);
//echo 'OAuth Token: ' . $_GET['oauth_token'];
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
$_SESSION['ot'] = $token->oauth_token;
$_SESSION['ots'] = $token->oauth_token_secret;
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
//echo '<pre>';
print_r($twitterInfo->response);
}
}
Update
I’ve exported the $twitterInfo variable and written it to a file and the output is below. Not sure if this helps at all.
EpiTwitterJson::__set_state(array( ‘resp’ =>
EpiCurlManager::__set_state(array(
‘key’ => ‘Resource id #12’,
‘epiCurl’ =>
EpiCurl::__set_state(array(
‘mc’ => NULL,
‘msgs’ => NULL,
‘running’ => NULL,
‘requests’ =>
array (
‘Resource id #11’ => NULL,
‘Resource id #12’ => NULL,
),
‘responses’ =>
array (
‘Resource id #11’ =>
array (
‘data’ => ‘ Invalid / expired Token
/oauth/access_token ‘,
‘code’ => 401,
‘time’ => 0.39,
‘length’ => 136,
‘type’ => ‘text/html; charset=utf-8’,
),
‘Resource id #12’ =>
array (
‘data’ => ‘{“error”:”Invalid \/ expired Token”,”request”:”\/account\/verify_credentials.json”}’,
‘code’ => 401,
‘time’ => 0.156,
‘length’ => 83,
‘type’ => ‘application/json; charset=utf-8’,
),
),
‘properties’ =>
array (
‘code’ => 2097154,
‘time’ => 3145731,
‘length’ => 3145743,
‘type’ => 1048594,
),
)), )), ‘responseText’ => ‘{“error”:”Invalid \/ expired Token”,”request”:”\/account\/verify_credentials.json”}’,
‘response’ => array (
‘error’ => ‘Invalid / expired Token’,
‘request’ => ‘/account/verify_credentials.json’, ), ‘error’ => ‘Invalid / expired Token’, ‘request’ => ‘/account/verify_credentials.json’, ))
Update 2
I’ve tried saving the access token by serialising the object and storing it in the database using the code below
function authenticate($consumer_key, $consumer_secret)
{
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$twitterObj->setToken($_GET['oauth_token']);
//echo 'OAuth Token: ' . $_GET['oauth_token'];
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
$_SESSION['ot'] = $token->oauth_token;
$_SESSION['ots'] = $token->oauth_token_secret;
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
//echo '<pre>';
//print_r($twitterInfo->response);
//var_dump($twitterInfo);
$username = $twitterInfo->screen_name;
$profilePic = $twitterInfo->profile_image_url;
addToDatabase($username, $profilePic, $_GET['oauth_token'], $_GET['oauth_verifier']);
}
function addToDatabase($username, $profilePic, $token, $verifier)
{
$token_serialised = serialize($token);
$query = "INSERT INTO social_sites (social_name, token, verifier, username, profilePicture) VALUES ('Twitter', '$token_serialised', '$verifier', "
. "'$username', '$profilePic')";
Then later on, I’m trying to post to twitter without it having to reload the twitter app authorise page by getting the value from the database I saved earlier and unserialising it to post to twitter using the following code:
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
while ($myrow = mysql_fetch_array($result))
{
//$access_token = unserialize($myrow['token']);
//$twitterObj->setToken($access_token);
//$token = $twitterObj->getAccessToken();
$token = unserialize($myrow['token']);
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
$_SESSION['ot'] = $token->oauth_token;
$_SESSION['ots'] = $token->oauth_token_secret;
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$myFile = "log.txt";
$fh = fopen($myFile, 'w') or die("Error");
fwrite($fh, var_export($twitterInfo->response, true));
fclose($fh);
// $twitterMsg = $_REQUEST['tweet'];
$update_status = $twitterObj->post_statusesUpdate(array('status' => $twitterMsg));
$tem = $update_status->response;
fwrite($fh, var_export($tem, true));
fclose($fh);
}
The log file writing is where is writing out the twitter response where it is stating that the token is invalid or expired.
Update 3
I’ve also noticed that it comes up with a php error when it unserializes it stating
Notice: unserialize(); error at offset 2555 of 2987 bytes
and when I try to export the variable it appears to be empty
How are you storing the oauth_token in the database?
You cannot store it simply as a string as it is an object. Instead you would need to serialize it before writing it to the database, and unserialize it when you retrieve it from the database.
http://php.net/manual/en/function.serialize.php