<?php
function saveTweets($screen_name) {
$db = array("h"=>"localhost", "u"=>"user", "p"=>"pass", "n"=>"db");
$dbconnect = mysql_connect($db['h'], $db['u'], $db['p']);
$dbselect = mysql_select_db($db['n']);
$screen_name = mysql_real_escape_string(strtolower(trim($screen_name)));
if (!$screen_name) {
echo "<p><strong>Error: No screen name declared.</strong></p>\n"; return false;
}
$row = mysql_query("SELECT `id` FROM `twitter` WHERE `screen_name`='$screen_name' ORDER BY `id` DESC LIMIT 1");
$row = mysql_fetch_array($row);
$last_id = $row['id'];
$url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$screen_name" ;
if ($last_id) {
$url .= "&since_id=$last_id" ;
}
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);
$affected = 0;
$twelement = new SimpleXMLElement($xml);
foreach ($twelement->status as $status) {
$text = mysql_real_escape_string(trim($status->text));
$time = strtotime($status->created_at);
$id = $status->id;
mysql_query("INSERT INTO `twitter` (`id`,`screen_name`,`time`,`text`,`hidden`) VALUES ('$id','$screen_name','$time','$text','n')");
$affected = $affected + mysql_affected_rows();
}
return "<p>".number_format($affected)." new tweets from $screen_name saved.</p>\n" ;
}
echo saveTweets('screenName');
?>
我正在尝试使用此脚本从我的 Twitter 提要中获取信息。但它返回从“screenName”保存的 0 条新推文。有人可以帮忙吗?
注意:我实际上用我和我的朋友的许多帐户替换了 screenName。