title: Twitter API not authorized when running from crontab
body: <p>I have set up OAuth and I am using CURL to get the latest tweet, this works perfectly when I run it from my browser</p>
  
  <pre><code>http://mydomain.com/getstatus.php
  </code></pre>
  
  <p>The script write statuses into a file:</p>
  
  <pre><code>$xTIME = time();
  $oauth = array(
    'oauth_consumer_key'     =&gt; $consumer_key,
    'oauth_nonce'            =&gt; $xTIME,
    'oauth_signature_method' =&gt; 'HMAC-SHA1',
    'oauth_token'            =&gt; $oauth_access_token,
    'oauth_timestamp'        =&gt; $xTIME,
    'oauth_version'          =&gt; '1.0'
  );
  
  $base_info                = buildBaseString($url, 'GET', $oauth);
  $composite_key            = rawurlencode($consumer_secret) . '&amp;' . rawurlencode($oauth_access_token_secret);
  $oauth_signature          = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  $oauth['oauth_signature'] = $oauth_signature;
  
  $header = array(buildAuthorizationHeader($oauth), 'Expect:');
  $options = array(
    CURLOPT_HTTPHEADER     =&gt; $header,
    CURLOPT_HEADER         =&gt; false,
    CURLOPT_URL            =&gt; $url,
    CURLOPT_RETURNTRANSFER =&gt; true,
    CURLOPT_SSL_VERIFYPEER =&gt; false
  );
  
  $feed = curl_init();
  curl_setopt_array($feed, $options);
  $json = curl_exec($feed);
  curl_close($feed);
  
  $twitter_data = json_decode($json); 
  
  if(isset($twitter_data[0]-&gt;text))
  {
    $fp = fopen('/var/www/mydomain.com/feeds/twitter.feed', 'w');
    fwrite($fp, $twitter_data[0]-&gt;text);
    fclose($fp);
  }
  </code></pre>
  
  <p>This writes to the file nicely, the idea is to have this run every hour. </p>
  
  <p>So I set up a cronjob </p>
  
  <pre><code>0 * * * * lynx -accept_all_cookies http://mydomain.com/getstatus.php
  </code></pre>
  
  <p>But I get this returned: </p>
  
  <pre><code>[request] =&gt; /1/statuses/user_timeline.json?count=1&amp;screen_name=twitter
  [error] =&gt; Rate limit exceeded. Clients may not make more than 150 requests per hour.
  </code></pre>
  
  <p>Which suggests it is not authorizing and so treating the request as an unauth'd request.</p>
  
  <p>My question is, why would it not be authorizing?</p>
  
  <p>I have tried running it as a crontab and running it through lynx and I get the same outcome both times.</p>
  
  <p>Edit:</p>
  
  <p>Here are the full functions,</p>
  
  <pre><code>function buildBaseString($baseURI, $method, $params)
  {
      $r = array(); 
      ksort($params);
      foreach($params as $key=&gt;$value){
          $r[] = "$key=" . rawurlencode($value); 
      }
      return $method."&amp;" . rawurlencode($baseURI) . '&amp;' . rawurlencode(implode('&amp;', $r)); 
  }
  
  function buildAuthorizationHeader($oauth)
  {
      $r = 'Authorization: OAuth '; 
      $values = array(); 
      foreach($oauth as $key=&gt;$value)
          $values[] = "$key=\"" . rawurlencode($value) . "\""; 
      $r .= implode(', ', $values); 
      return $r; 
  }
  
  function loadTW($twID)
  {
      $url = "https://api.twitter.com/1/statuses/user_timeline.json&amp;count=1&amp;screen_name=" .$twID;
  
      $oauth_access_token = "68161130-X4HRZje9wB3lpyeOPYDJPsqG1JfJxxxxxxxx";
      $oauth_access_token_secret = "zN98CUldHN4eiVeGahZIvpNeUGljRTxxxxxxxx";
      $consumer_key = "PeVEz2Z0QSKtxxxxxxx";
      $consumer_secret = "An9Xh3qHHTEiTQzW5wKLFMHOrbzwFtwxxxxxxxx";
  
      $xTIME = time();
      $oauth = array( 'oauth_consumer_key' =&gt; $consumer_key,
                      'oauth_nonce' =&gt; $xTIME,
                      'oauth_signature_method' =&gt; 'HMAC-SHA1',
                      'oauth_token' =&gt; $oauth_access_token,
                      'oauth_timestamp' =&gt; $xTIME,
                      'oauth_version' =&gt; '1.0');  
  
  
      $base_info = buildBaseString($url, 'GET', $oauth);
  
      $composite_key = rawurlencode($consumer_secret) . '&amp;' . rawurlencode($oauth_access_token_secret);
  
      $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  
      $oauth['oauth_signature'] = $oauth_signature;                       
  
      $header = array(buildAuthorizationHeader($oauth), 'Expect:');
      $options = array( CURLOPT_HTTPHEADER =&gt; $header,
                        CURLOPT_HEADER =&gt; false,
                        CURLOPT_URL =&gt; $url,
                        CURLOPT_RETURNTRANSFER =&gt; true,
                        CURLOPT_SSL_VERIFYPEER =&gt; false);
  
      $feed = curl_init();
      curl_setopt_array($feed, $options);
      $json = curl_exec($feed);
      curl_close($feed);
  
      $twitter_data = json_decode($json); 
  
      if(isset($twitter_data[0]-&gt;text))
      {
          $fp = fopen('/var/www/carlandalexfishing.co.uk/feeds/twitter.feed', 'w');
          fwrite($fp, $twitter_data[0]-&gt;text);
          fclose($fp);
      }
  }
  </code></pre>