这是获取推文的代码,但我需要在PHP中使用它,任何人都可以提供任何见解吗?
$(document).ready( function() {
var url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?";
$.getJSON(url,
function(data){
$.each(data, function(i, item) {
$("#twitter-posts").append("<p>" + item.text.linkify() + " <span class='created_at'>" + relative_time(item.created_at) + " via " + item.source + "</span></p>");
});
});
});
String.prototype.linkify = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
return m.link(m);
});
};
function relative_time(time_value) {
var values = time_value.split(" ");
time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
var parsed_date = Date.parse(time_value);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
delta = delta + (relative_to.getTimezoneOffset() * 60);
var r = '';
if (delta < 60) {
r = 'a minute ago';
} else if(delta < 120) {
r = 'couple of minutes ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60)).toString() + ' minutes ago';
} else if(delta < (90*60)) {
r = 'an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = '1 day ago';
} else {
r = (parseInt(delta / 86400)).toString() + ' days ago';
}
return r;
}
function twitter_callback ()
{
return true;
}
答案 0 :(得分:2)
您可以使用cURL访问该文件。然后使用PHP函数json_decode
来处理数据。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result);
var_dump($json);
答案 1 :(得分:2)
最复杂的部分是对twitter的请求,你可以这样做:
$content = file_get_contents("http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?");
$twitter_response = json_decode($content);
foreach($twitter_response as $item){
//format someway
$item->text; #get the text of each tweet
}
答案 2 :(得分:1)
是的,你可以做到。对于主要部分,您可以使用cURL或file_get_contents来获取数据,使用json_decode来解析它。