2012-05-25 18 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 
}