Passing Extra Parameters to Twitter via OAuth

A while ago I wrote a post explaining how to pull data from Twitter's API. I focused on pulling the default authenticated user timeline (which, if you set up the application under your account, you are the authenticated user). Wouldn't it be great if you could customize the request a bit?

The default feed will have the most recent 20 tweets from you. If you want to pull a different user or a different amount of tweets you'll have to pass in some parameters with your request. Note: protected timelines can only be pulled by the owner or their followers. There are two parameters that we'll need to use… screen_name and count.

Building off my first post, to pass in additional paramters you'll need to change a few lines in the OAuth Signature.

  1. $oauth_hash = '';

  2. $oauth_hash .= 'count=TOTAL_COUNT_YOU_WANT&';

  3. $oauth_hash .= 'oauth_consumer_key=YOUR_CONSUMER_KEY&';

  4. $oauth_hash .= 'oauth_nonce=' . time() . '&';

  5. $oauth_hash .= 'oauth_signature_method=HMAC-SHA1&';

  6. $oauth_hash .= 'oauth_timestamp=' . time() . '&';

  7. $oauth_hash .= 'oauth_token=YOUR_ACCESS_TOKEN&';

  8. $oauth_hash .= 'oauth_version=1.0&';

  9. $oauth_hash .= 'screen_name=SCREEN_NAME_HERE';

Another change you need to make is with the OAuth header, again adding the two new parameters.

  1. $oauth_header = '';

  2. $oauth_header .= 'count="TOTAL_COUNT_YOU_WANT", ';

  3. $oauth_header .= 'oauth_consumer_key="YOUR_CONSUMER_KEY", ';

  4. $oauth_header .= 'oauth_nonce="' . time() . '", ';

  5. $oauth_header .= 'oauth_signature="' . $signature . '", ';

  6. $oauth_header .= 'oauth_signature_method="HMAC-SHA1", ';

  7. $oauth_header .= 'oauth_timestamp="' . time() . '", ';

  8. $oauth_header .= 'oauth_token="YOUR_ACCESS_TOKEN", ';

  9. $oauth_header .= 'oauth_version="1.0", ';

  10. $oauth_header .= 'screen_name="SCREEN_NAME_HERE"';

The last change is with the curl url itself.

  1. curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=TOTAL_COUNT_YOU_WANT&screen_name=SCREEN_NAME_HERE');

There are other parameters you could pass in as well, including exclude_replies, max_id, and more. You would just follow the same changes for each new field. When I was testing this I noticed that Twitter seemed to want the parameters in alphabetical order - not sure if that was a temp issue or is still the case, but it's not hard to set things up that way. To see the other parameters, check out Twitter's documentation on user_timeline.