2012-05-25 115 views
3

如何检测链接上的默认头像是这样的:https://graph.facebook.com/'.$id.'/picture?type=large? 它是从特殊准备的配置文件中获得化身(男性/女性)的唯一方法,然后通过比较md5()?如何检测Facebook上的默认头像?

很难相信这是唯一的方法。

+1

有用户的个人资料图片,你已经知道网址,除了那个之外你还需要什么? –

回答

5

没有可以调用的API来判断它们是否使用默认的照片。而不是下载整个图像和检查MD5的,你可以发出HTTP HEAD请求到个人资料网址,并期待在Location头,看看URL是已知的默认配置文件图像之一:

男:https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yL/r/HsTZSDw4avx.gif https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yL/r/HsTZSDw4avx.gif

女(达斯维德):https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yp/r/yDnr5YfbJCH.gif

enter image description here

这些网址可能会改变我猜想,所以可以在默认的照片,但我还没有看到任何一种情况下发生的I C记住。

+6

我提高了达斯维德评论的这个cuz。做得好! – threejeez

+3

只是使用is_silhouette – Fattie

+0

达斯维德评论岩! – Prozi

0

如果你已经开始对图形API的调用来得到像头像的用户数据,只包括在fields PARAM picture当您第一次调用图形API ,那么响应将包括is_silhouette偏移量,如果它设置为true,则用户具有默认头像。

请求:

https://graph.facebook.com/v2.7/me?access_token=[token]&fields=name,picture 

响应:

{ 
    "id": "100103095474350", 
    "name": "John Smith", 
    "picture": { 
     "data": { 
      "is_silhouette": true, 
      "url": "https://scontent.xx.fbcdn.net/v/...jpg" 
     } 
    } 
} 
0

使用Facebook SDK适用于iOS(SWIFT 4):

class FacebookSignIn { 

    enum Error: Swift.Error { 
     case unableToInitializeGraphRequest 
     case unexpectedGraphResponse 
    } 

    func profileImageURL(size: CGSize, completion: @escaping Result<URL?>.Completion) { 
     guard let userID = FBSDKAccessToken.current()?.userID else { 
      completion(.failure(Error.unableToInitializeGraphRequest)) 
      return 
     } 
     let params: [String: Any] = ["redirect": 0, 
            "type": size.width == size.height ? "square" : "normal", 
            "height": Int(size.height), 
            "width": Int(size.width), 
            "fields": "is_silhouette, url"] 

     guard let request = FBSDKGraphRequest(graphPath: "/\(userID)/picture", parameters: params) else { 
      completion(.failure(Error.unableToInitializeGraphRequest)) 
      return 
     } 

     _ = request.start { _, result, error in 
      if let e = error { 
      completion(.failure(e)) 
      } else if let result = result as? [String: Any], let data = result["data"] as? [String: Any] { 
      if let isSilhouette = data["is_silhouette"] as? Bool, let urlString = data["url"] as? String { 
       if isSilhouette { 
        completion(.success(nil)) 
       } else { 
        if let url = URL(string: urlString) { 
         completion(.success(url)) 
        } else { 
         completion(.failure(Error.unexpectedGraphResponse)) 
        } 
       } 
      } else { 
       completion(.failure(Error.unexpectedGraphResponse)) 
      } 
      } else { 
      completion(.failure(Error.unexpectedGraphResponse)) 
      } 
     } 
    } 
} 


public enum Result<T> { 

    case success(T) 
    case failure(Swift.Error) 

    public typealias Completion = (Result<T>) -> Void 
}