Created
April 24, 2011 04:33
-
-
Save miya0001/939309 to your computer and use it in GitHub Desktop.
コマンドライン引数で指定したユーザーの最新のツイートをfacebookのウォールに投稿する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(dirname(__FILE__).'/src/facebook.php'); // facebook SDK for php | |
define("TOKEN", "YOUR ACCESS TOKEN"); | |
define("API", "http://twitter.com/statuses/user_timeline/%s.json"); | |
define("LINK_URL", "http://twitter.com/#!/%s/status/%s"); | |
$facebook = new Facebook(array( | |
'appId' => 'YOUR APP ID', | |
'secret' => 'YOUR SECRET KEY', | |
'cookie' => false, | |
)); | |
if (!is_dir(dirname(__FILE__).'/cache')) { | |
mkdir(dirname(__FILE__).'/cache', 0755); | |
} | |
if (isset($argv[1]) && $argv[1]) { | |
$url = sprintf(API, addslashes($argv[1])); | |
} else { | |
exit; // twitter user not set | |
} | |
$json = @file_get_contents($url); | |
if (!$json) { | |
exit; // error on twitter api | |
} | |
$tweets = json_decode($json); | |
$name = $tweets[0]->user->screen_name; | |
$id = $tweets[0]->id_str; | |
if ($id === file_get_contents(dirname(__FILE__).'/cache/tweet.log')) { | |
exit; // latest tweet already posted to facebook | |
} | |
$link = sprintf(LINK_URL, $name, $id); | |
try { | |
$res = $facebook->api( | |
"/me/feed", | |
"POST", | |
array( | |
"access_token" => TOKEN, | |
"link" => $link, | |
) | |
); | |
file_put_contents(dirname(__FILE__).'/cache/tweet.log', $id); | |
} catch(FacebookApiException $e) { | |
var_dump($e->getResult()); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment