Graph API - FB - 已经变得疯狂了。 :s - 获取个人资料图片

问题描述 投票:0回答:2

我可以从中获取个人资料图片:

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时才使用fb.from.id。

关于2),请注意,如果我评论该行:

//html += "<img id=\"image"+i+"\" src=\"http://graph.facebook.com/" + fb.from.id + "/picture\"/>";

不会显示任何图像,并且所有帖子信息(个人资料图片除外)都会显示在视口上。

如果我只处理该图的第一部分(带有“来自:”的部分),我会得到该帖子的图片。

所以,问题确实应该出在第二张图上。那个带有“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,我可能需要使用另一种语法?

json facebook facebook-graph-api
2个回答
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
投票

您可以尝试一下 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。

© www.soinside.com 2019 - 2024. All rights reserved.