title: Updating twitter status from php
body: <p>I am working on a PHP project where I want to be able to post things automatically to twitter. </p>
  
  <p>From my PHP program I have authorised my twitter app and stored the oauth_token and verifier in a mysql database. </p>
  
  <p>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 </p>
  
  <blockquote>
    <p>'invalid/expired token'</p>
  </blockquote>
  
  <p>Below is the code I am using</p>
  
  <pre><code>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-&gt;setToken($oauth_token);
              //echo 'OAuth Token: ' . $_GET['oauth_token'];
              $token = $twitterObj-&gt;getAccessToken();
              $twitterObj-&gt;setToken($token-&gt;oauth_token, $token-&gt;oauth_token_secret);
              $_SESSION['ot'] = $token-&gt;oauth_token;
              $_SESSION['ots'] = $token-&gt;oauth_token_secret;
              $twitterInfo= $twitterObj-&gt;get_accountVerify_credentials();
              $twitterInfo-&gt;response;
              //echo '&lt;pre&gt;';
              print_r($twitterInfo-&gt;response);
  
          }
      }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I've exported the <code>$twitterInfo</code> variable and written it to a file and the output is below. Not sure if this helps at all.</p>
  
  <blockquote>
    <p>EpiTwitterJson::__set_state(array(    'resp' =><br>
    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<br>
    /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"}',<br>
    'response' =>    array (
        'error' => 'Invalid / expired Token',
        'request' => '/account/verify_credentials.json',   ),    'error' => 'Invalid / expired Token',    'request' => '/account/verify_credentials.json', ))</p>
  </blockquote>
  
  <h2>Update 2</h2>
  
  <p>I've tried saving the access token by serialising the object and storing it in the database using the code below</p>
  
  <pre><code>function authenticate($consumer_key, $consumer_secret)
      {
          $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
  
          $twitterObj-&gt;setToken($_GET['oauth_token']);
          //echo 'OAuth Token: ' . $_GET['oauth_token'];
          $token = $twitterObj-&gt;getAccessToken();
          $twitterObj-&gt;setToken($token-&gt;oauth_token, $token-&gt;oauth_token_secret);
          $_SESSION['ot'] = $token-&gt;oauth_token;
          $_SESSION['ots'] = $token-&gt;oauth_token_secret;
          $twitterInfo= $twitterObj-&gt;get_accountVerify_credentials();
          $twitterInfo-&gt;response;
          //echo '&lt;pre&gt;';
          //print_r($twitterInfo-&gt;response);
          //var_dump($twitterInfo);
          $username = $twitterInfo-&gt;screen_name;
          $profilePic = $twitterInfo-&gt;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')";
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
              while ($myrow = mysql_fetch_array($result))
              {
                  //$access_token = unserialize($myrow['token']);
                  //$twitterObj-&gt;setToken($access_token);
                  //$token = $twitterObj-&gt;getAccessToken();
                  $token = unserialize($myrow['token']);
                  $twitterObj-&gt;setToken($token-&gt;oauth_token, $token-&gt;oauth_token_secret);
                  $_SESSION['ot'] = $token-&gt;oauth_token;
                  $_SESSION['ots'] = $token-&gt;oauth_token_secret;
                  $twitterInfo= $twitterObj-&gt;get_accountVerify_credentials();     
  
                  $myFile = "log.txt";
                  $fh = fopen($myFile, 'w') or die("Error");
                  fwrite($fh, var_export($twitterInfo-&gt;response, true));
                  fclose($fh);
  
  //              $twitterMsg = $_REQUEST['tweet'];
                  $update_status = $twitterObj-&gt;post_statusesUpdate(array('status' =&gt; $twitterMsg));
                  $tem = $update_status-&gt;response;    
                  fwrite($fh, var_export($tem, true));
                  fclose($fh);
  }
  </code></pre>
  
  <p>The log file writing is where is writing out the twitter response where it is stating that the token is invalid or expired.</p>
  
  <h2>Update 3</h2>
  
  <p>I've also noticed that it comes up with a php error when it unserializes it stating </p>
  
  <blockquote>
    <p>Notice: unserialize(); error at offset 2555 of 2987 bytes</p>
  </blockquote>
  
  <p>and when I try to export the variable it appears to be empty</p>