Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7553045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:56:31+00:00 2026-05-30T10:56:31+00:00

I am working on a PHP project where I want to be able to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:56:33+00:00Added an answer on May 30, 2026 at 10:56 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on my first PHP project, and it's going well. I've been able
I am using MySQL and PHP for a project I am working. I have
Im working on a small PHP project for learning purpose. i want to retrieve
I'm working on a new php project with CodeIgniter and I have few questions
I am working on a new PHP project now, this time I want to
What do I want to achieve? We are currently working on a PHP project
With my recent post regarding a project I have been working on, I have
I am currently working on an html and php project. What I want to
I am currently working on an HTML, CSS, PHP project. I want to display
I am on Linux, working on forking a PHP project. I want to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.