Advertisement
cornedor

Twitter API 1.1

Jun 13th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. /* Make a developers account on twitter, create an app and get the consumer key/secret. */
  2. $key = '[key here]';
  3. $secret = '[secret here'];
  4. $postdata = http_build_query(array('grant_type' => 'client_credentials'));
  5. $opts = array('http' => array('method'  => 'POST',
  6.                 'header'  => "Authorization: Basic ".base64_encode($key . ':' . $secret)."\r\nContent-Type: application/x-www-form-urlencoded;charset=UTF-8",
  7.                 'content' => $postdata));
  8. $context = stream_context_create($opts);
  9. $data =  json_decode(file_get_contents('https://api.twitter.com/oauth2/token', false, $context));
  10. echo $data->access_token;
  11.  
  12. /* get this access_token and write it down somewhere. */
  13.  
  14. /* Now to use the Twitter API */
  15. $access_token = '[access code here]';
  16. $opts = array('http' => array('method'  => 'GET',
  17.                                       'header'  => "Authorization: Bearer " . $access_token . "\r\nContent-Type: application/x-www-form-urlencoded;charset=UTF-8"));
  18. $context = stream_context_create($opts);   
  19. $data = json_decode(file_get_contents('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2', false, $context));
  20. var_dump($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement