<?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 Feed中获取。但它返回从'screenName'保存的0条新推文。有人可以帮忙吗?
注意:我实际上用我和朋友的许多帐户替换了screenName。
答案 0 :(得分:3)
在推出twitter api 1.0之后,现在几乎所有来自twitter的数据都是通过身份验证。您可以通过以下链接阅读所有详细信息 Twitter user timeline
答案 1 :(得分:2)
您的代码无效,因为您使用的是已弃用的API。以下是Feed的输出。
<errors>
<error code="68">
The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.
</error>
</errors>
答案 2 :(得分:2)