我可以从中获取个人资料图片:
data": [
{
"id": "11111111111_22222222222",
"from": {
"name": "Some Name",
"category": "Non-profit organization",
"id": "11222131212211"
},
我这样做:
$.each(json.data,function(i,fb){
html += "<img id=\"image"+i+"\" src=\"http://graph.facebook.com/" + fb.from.id + "/picture\"/>";
}
没问题。
但是,稍后在图表上我有:
"id": "11111111111_2222222222",
"to": {
"data": [
{
"name": "Some Name",
"category": "Non-profit organization",
"id": "11222131212211"
我需要抓住这个ID,但我尝试过:
alert(fb.to.data.id);
什么也没得到。
如果我这样做:alert(fb.to)
我得到了“未定义”。
有没有人遇到过这类问题。正如您可能已经注意到我对编程问题一直不太熟悉,但是,我会尽力解决这个问题。
1)如何在第二种情况下显示个人资料图像? 2)我怎么说:只有当图表有这个时才使用fb.from.id.
关于2),请注意,如果我评论该行:
//html += "<img id=\"image"+i+"\" src=\"http://graph.facebook.com/" + fb.from.id + "/picture\"/>";
不会显示任何图像,并且视口上会显示所有帖子信息(个人资料图片除外)。
如果我只处理此图表的第一部分(带有“from:”的那一部分),我会得到该帖子的图片。
所以,问题必须确实在第二个图形部分。带有“to:”的那个我无法获取ID。
我也试过避免在第二种情况下渲染图像,但至少运行第一种情况。一点也不运气。
if (fb.from.id != undefined) {
//do something;
}
图表的第二部分仍然会返回错误。
由于所有这些混乱,我可以重新安排,我可以请你帮忙。 :S
**
更新
**
js代码:
function fbFetch(){
var url = "https://graph.facebook.com/125120007543580/feed&callback=?&limit=2";
var i = 0;
//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
var html = "<div id=\"faceWall\">";
//loop through and within data array's retrieve the message variable.
$.each(json.data,function(i,fb){
var idTotal = fb.id;
var ids = idTotal.split("_");
var href = "http://www.facebook.com/"+ids[0]+"/posts/"+ids[1];
var msg = fb.message;
//adicionado
if (msg == undefined) {
msg = 'Cick here to see the post title';
} else if (msg.length > 150) {
msg = msg.substr(0,200)+"...";
}
//ISSUE HERE. IF I COMMENT THIS LINE ALL GOES WELL BUT NO PROFILE PICTURES ARE DISPLAYED.
html += "<img id=\"imagem"+i+"\" src=\"http://graph.facebook.com/" + fb.from.id + "/picture\"/>";
html += "<div id=\"textoFaceWall\">";
html += "<p id=\"msg"+i+"\">";
//adicionado fb.name em vez de fb.from.name:
if (fb.name == undefined) {
html += "<a href=\""+href+"\" target=\"_blank\">" + fb.from.name + "</a> - " + msg + "</p>";
} else {
html += "<a href=\""+href+"\" target=\"_blank\">" + fb.name + "</a> - " + msg + "</p>";
}
html += "<p class=\"dataPostWall\" id=\"data"+i+"\">"+ dataFinal + "</p> ";
html += "</p>";
html += "</div>";
html += "<img class=\"linhaHomePageCanais\" src=\""+baseUrl+"/lib/img/linhaWall.png\" alt=\"linha wall\"/>";
});
html += "</div>";
$("#coluna2").append(html);
});
};
fbFetch();
图表的一部分:
({
"data": [
{
"id": "125120007543580_150880001634837",
"from": {
"name": "Some Name",
"category": "Non-profit organization",
"id": "125120007543580"
},
{
"id": "125120007543580_111122368963254",
"to": {
"data": [
{
"name": "Some Name",
"category": "Non-profit organization",
"id": "125120007543580"
}
]
},
我需要显示来自fb.to.data.id的个人资料, 我现在注意到,数据有 [而不是 {,这可能意味着,为了访问id,我需要使用其他语法吗?
答案 0 :(得分:3)
由于to
参数可以容纳更多一个用户,因此它是一个对象数组。所以你也需要循环:
if(fb.to) {
$.each(fb.to.data, function(j,to_user) {
// now you access it to_user.id
});
}
如果您只想显示第一张个人资料照片,请使用fb.to.data[0].id
。
修改强>
好的,根据您的评论和更新,这是您的代码与工作方法:
function fbFetch(){
var url = "https://graph.facebook.com/125120007543580/feed&callback=?&limit=2";
var i = 0;
//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
var html = "<div id=\"faceWall\">";
//loop through and within data array's retrieve the message variable.
$.each(json.data,function(i,fb){
var idTotal = fb.id;
var ids = idTotal.split("_");
var href = "http://www.facebook.com/"+ids[0]+"/posts/"+ids[1];
var msg = fb.message;
//adicionado
if (msg == undefined) {
msg = 'Cick here to see the post title';
} else if (msg.length > 150) {
msg = msg.substr(0,200)+"...";
}
//ISSUE HERE. IF I COMMENT THIS LINE ALL GOES WELL BUT NO PROFILE PICTURES ARE DISPLAYED.
if(fb.from)
html += "<img id=\"imagem"+i+"\" src=\"http://graph.facebook.com/" + fb.from.id + "/picture\"/>";
if(fb.to) {
$.each(fb.to.data, function(j,to_user) {
html += "<img id=\"imagem"+i+"-"+j+"\" src=\"http://graph.facebook.com/" + to_user.id + "/picture\"/>";
});
}
html += "<div id=\"textoFaceWall\">";
html += "<p id=\"msg"+i+"\">";
//adicionado fb.name em vez de fb.from.name:
if (fb.name == undefined) {
html += "<a href=\""+href+"\" target=\"_blank\">" + fb.from.name + "</a> - " + msg + "</p>";
} else {
html += "<a href=\""+href+"\" target=\"_blank\">" + fb.name + "</a> - " + msg + "</p>";
}
html += "<p class=\"dataPostWall\" id=\"data"+i+"\">"+ dataFinal + "</p> ";
html += "</p>";
html += "</div>";
html += "<img class=\"linhaHomePageCanais\" src=\""+baseUrl+"/lib/img/linhaWall.png\" alt=\"linha wall\"/>";
});
html += "</div>";
$("#coluna2").append(html);
});
};
fbFetch();
答案 1 :(得分:1)
您可以尝试使用PHP
$request_url = 'http://graph.facebook.com/redbull/picture?redirect=false';
$requests = file_get_contents($request_url);
$fb_response = json_decode($requests);
$imagen[]=$fb_response->data->url;
您可以从JSON Graph api调用中的URL项获取图片的URL。