Friday, April 10, 2015

[PHP] Twitter GET API simple

10 April 2015

Intro::

Simple GET timeline example buat twitter API via PHP, menggunakan script Abraham.

Steps::

Buat Applikasi Twitter untuk mendapatkan token & secret code
1. https://apps.twitter.com/
2. copy & catat, consumer key, consumer secret, access token, access token secret

Download Script TwitterOAuth PHP (versi Abraham)
1. https://github.com/abraham/twitteroauth
2. install & extract localhost/twitplay/lib/twitteroauth/
3. copas script dibawah ini dan jangan lupa mengganti consumer keyconsumer secretaccess tokenaccess token secret

<?php include "lib/twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$consumer = "consumer key";
$consumersecret = "consumer secret";
$accesstoken = "access token";
$accesstokensecret = "access token secret";
$twitter = new TwitterOAuth($consumer, $consumersecret, $accesstoken, $accesstokensecret);
$content = $twitter->get("account/verify_credentials");

//query twitter
$tweets = $twitter->get("statuses/user_timeline",
array( 'screen_name' => 'radenfaris',
'count'=>20,
));

$c=1;

foreach($tweets as $tweet){
echo $c.'. '.$tweet->text.'<br>';
$c++;
}


?>

4. simpan mytweet.php dan panggil dengan /localhost/twitplay/mytweet.php

Catatan::

Twitter Rest API : https://dev.twitter.com/rest/public

No comments:

Post a Comment