首页 文章

Facebook Graph API object_id有时会丢失

提问于
浏览
2

我正在使用Facebook Graph API来获取用户的新闻源 .

我的请求网址是:xxxxxxxxxxxxxx / feed?fields = from,id,created_time,picture,link,object_id,message,likes.fields(id)

使用object_id,我想使用以下URL获取帖子的大图:http://graph.facebook.com/OBJECT_ID/picture?type=normal

图片返回字段始终填充,但在某些帖子中未返回object_id . 为什么是这样?我真的需要高分辨率的图片,并没有找到另一种方式来获得这个..

3 回答

  • 1

    仅当附件是facebook对象(例如,用户上传的图像)时才返回object_id . Feed中的某些故事根本没有图片,有些图片不是facebook对象(例如,共享链接的缩略图) .

  • 0

    有时,Facebook会保留图像的缩略图,并在图形请求返回的URL中存储指向较大版本图像的外部链接 . 为了在任何一种情况下访问图像,我使用下面的代码,其中smallURL是图形请求返回的URL:

    private String getRealURL(String smallURL){
       if (smallURL.contains("url=http")){
           String[] pieces = smallURL.split("url=");
           pieces[1].replace("%2F", "//");
           pieces[1].replace("%3A", ":");
           return pieces[1];           
       }
       else{
           StringBuilder stringBuilder = new StringBuilder();
           stringBuilder.setLength(0);
           stringBuilder.append("http://graph.facebook.com/");
           stringBuilder.append(item.getObjectID());
           stringBuilder.append("/picture?type=large");
           return stringBuilder.toString();
       }
    }
    
  • 0

    我还注意到一些FB帖子对于大型照片没有 {object_id} ,但意识到 {picture} 缩略图URL包含原始大图像的编码URL:

    https://external.xx.fbcdn.net/safe_image.php?d=AQBe9UvGd0vPbAHP&w=130&h=130&url=http%3A%2F%2Fskift.com%2Fwp-content%2Fuploads%2F2015%2F12%2Fpollution.jpg&cfs=1
    
    • 包含 - >

    http://skift.com/wp-content/uploads/2015/12/pollution.jpg
    

    所以我检查 {object_id} ,如果没有,那么尝试从 {picture} 中提取原始URL:

    if(isset($post['object_id'])) {
        echo "http://graph.facebook.com/".$post['object_id']."/picture";
        }
        elseif(isset($post['picture'])) {
            echo urldecode(preg_replace('/&cfs.*/', '', preg_replace('/.*url=/', '', $post['picture'])));
        } 
        else {
            echo "no_large_image";
    }
    

相关问题