Intro::
Simple POST update status 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/
Simple Post Update
1. copas script dibawah ini dan jangan lupa mengganti consumer key, consumer secret, access token, access 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");
//POST UPDATE
$update = 'Testing post via API cc: @radenfaris';
if (strlen($update) < 140){
$updates = $twitter->post("statuses/update", array("status" => $update));
echo "Success updateted - ".$update;
}
?>
2. simpan posttweet.php dan panggil dengan /localhost/twitplay/posttweet.php
Simple Post Update dengan image
1. copas script dibawah ini dan jangan lupa mengganti consumer key, consumer secret, access token, access 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");
//tweets
$update = 'Test update via API dengan image cc: @radenfaris';
//image path
$image = 'img/indah.jpg';
if (strlen($update) < 120){
$media = $twitter->upload('media/upload', array('media' => $image));
$param = array(
'status' => $update,
'media_ids' => implode(',', array($media->media_id_string)),
);
$updates = $twitter->post('statuses/update', $param);
echo "Success updateted - ".$update;
}
?>
2. simpan postimage.php dan panggil dengan /localhost/twitplay/postimage.php
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/
Simple Post Update
1. copas script dibawah ini dan jangan lupa mengganti consumer key, consumer secret, access token, access 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");
//POST UPDATE
$update = 'Testing post via API cc: @radenfaris';
if (strlen($update) < 140){
$updates = $twitter->post("statuses/update", array("status" => $update));
echo "Success updateted - ".$update;
}
?>
2. simpan posttweet.php dan panggil dengan /localhost/twitplay/posttweet.php
Simple Post Update dengan image
1. copas script dibawah ini dan jangan lupa mengganti consumer key, consumer secret, access token, access 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");
//tweets
$update = 'Test update via API dengan image cc: @radenfaris';
//image path
$image = 'img/indah.jpg';
if (strlen($update) < 120){
$media = $twitter->upload('media/upload', array('media' => $image));
$param = array(
'status' => $update,
'media_ids' => implode(',', array($media->media_id_string)),
);
$updates = $twitter->post('statuses/update', $param);
echo "Success updateted - ".$update;
}
?>
2. simpan postimage.php dan panggil dengan /localhost/twitplay/postimage.php
Catatan::
yupi.jpg jangan lupa dimasukan kedalam folder /img/Twitter Rest API : https://dev.twitter.com/rest/publicTwitterOAuth PHP Abraham : https://github.com/abraham/twitteroauth
No comments:
Post a Comment