yuheijotaki.com

自分のツイートを取得する TwistOAuth / Twitter API

<?php
// Load this library.
require 'TwistOAuth.phar';
// Prepare simple wrapper function for htmlspecialchars.
function h($str) {
	return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
// Set default HTTP status code.
$code = 200;
// Set your timezone.
date_default_timezone_set('Asia/Tokyo');
try {
	$to = new TwistOAuth('XXXXXXXXXX', 'XXXXXXXXXX', 'XXXXXXXXXX', 'XXXXXXXXXX');
	$statuses = $to->get('statuses/user_timeline', array('count' => 1));
} catch (TwistException $e) {
	// Set error message.
	$error = $e->getMessage();
	// Overwrite HTTP status code.
	// The exception code will be zero when it thrown before accessing Twitter, we need to change it into 500.
	$code = $e->getCode() ?: 500;
}
?>
<?php if (!empty($statuses)): ?>
<?php foreach ($statuses as $status): ?>
<?php
	$tweet_id = $status->id_str;
	$tweetUrl = 'https://twitter.com/username' . '/status/' . $tweet_id;
?>
<?php $tweetText = $status->text; ?>
<?php endforeach; ?>
<?php endif; ?>