开发者

How can I get Facebook Profile image from email?

开发者 https://www.devze.com 2022-12-30 08:46 出处:网络
There\'s an outlook plugin called Xobni that has a really cool feature, if a contact has an email address, it will fetch that contact\'s profile picture and display it. Their FAQ states the following:

There's an outlook plugin called Xobni that has a really cool feature, if a contact has an email address, it will fetch that contact's profile picture and display it. Their FAQ states the following:

Xobni sends an encrypted email address to Facebook to retrieve the Facebook profile for the person who is currently being viewed in the Xobni sidebar. Your own Facebook profile is never altered by Xobni, and all Facebook privacy settings are strictly followed when viewing other profiles.

I'd like to duplicate this functionality. However, I can't figure out which API call they're using. I'm assuming when they say "encrypted email address" that's laymen's terms for the emai开发者_JAVA技巧l hash. Once a username is derived, the graph api looks ideal for actually fetching the image, but I'm having trouble going from email hash to profile ID.


You can query the following URL to get user id (if one exists on Facebook):

https://graph.facebook.com/search?access_token=YOUR_ACCESS_TOKEN&q=EMAIL_ADDRESS_URL_ENCODED&type=user

Then <img src="https://graph.facebook.com/USER_ID/picture"> gives you the picture.

More info: article at codinglogs.com


I am searching for a way to do this exact thing... No attempts have worked yet.

Has anyone been able to find a solution?

Update: I've put together this snippet in PHP. It's just about the only way I've been able to accomplish my goal. I'm not sure how Xobni is doing it (I'm sure they are less intrusive about it)

<?php

/* Email to Search By */
$eml = 'user@domain.com';

/* This is where we are going to search.. */
$url = 'http://www.facebook.com/search.php?q=' . urlencode($eml);

/* Fetch using cURL */
$ch = curl_init();

/* Set cURL Options */
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* Tell Facebook that we are using a valid browser */
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');

/* Execute cURL, get Response */
$response = curl_exec($ch);

/* Check HTTP Code */
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

/* Close cURL */
curl_close($ch);

/* 200 Response! */
if ($response_code == 200) {

    /* Parse HTML Response */
    $dom = new DOMDocument();
    @$dom->loadHTML($response);

    /* What we are looking for */
    $match = 'http://www.facebook.com/profile.php?id=';

    /* Facebook UIDs */
    $uids = array();

    /* Find all Anchors */
    $anchors = $dom->getElementsByTagName('a');
    foreach ($anchors as $anchor) {
        $href = $anchor->getAttribute('href');
        if (stristr($href, $match) !== false) {
            $uids[] = str_replace($match, '', $href);
        }
    }

    /* Found Facebook Users */
    if (!empty($uids)) {

        /* Return Unique UIDs */
        $uids = array_unique($uids);

        /* Show Results */
        foreach ($uids as $uid) {

            /* Profile Picture */
            echo '<img src="http://graph.facebook.com/' . $uid. '/picture" alt="' . $uid . '" />';

        }

    }

}


?


It is no longer possible to search user info with email address via Facebook Graph API. While it still works if you have the Facebook user ID, but if you can't get the Facebook ID with the search API, you can no longer do this.

https://developers.facebook.com/x/bugs/453298034751100/

The API will return the following response:

{
   "error": {
       "message": "(#200) Must have a valid access_token to access this endpoint",
       "type": "OAuthException",
       "code": 200
   }
}


Many thanks @McHerbie, you gave me the clue to FINALLY get my code working. The key is the urlencode() function to encode email!!! thanks, this is my working code using PHP Simple HTML Dom Parser:

public function getFacebookPictureByScrapping($email="your@email.com", $file="fbPicture.jpg") {
            require_once('protected/extensions/simplehtmldom/simple_html_dom.php');
            $userEmail = urlencode($email);

            ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');

            $url = "http://www.facebook.com/search.php?q=$userEmail&type=all&init=srp";
            $html = new simple_html_dom();
            $html = file_get_html($url);

            if (is_object($picture = $html->find(".uiList .img",0))) {
                $image = file_get_contents($picture->src, false);
                file_put_contents($file);
                return $file;
            } else {
                return null;
            }
    }


I know this post is a bit old, but only just now found it - you might try the FullContact Person API (full disclosure - I'm biased, I work for them):

http://www.fullcontact.com/developer/

On the one hand, it will pull the associated social media profiles when you query based on email so that you can find and pull the associated profile...but on the other hand, you can also save some time & use it to pull the profile images directly.

The response schema includes:

    "photos": 
  [
    {
      "typeId": [photo type],
      "typeName": [photo type name],
      "url": [photo url],
      "isPrimary": [boolean]
    }
  ]

More info: http://www.fullcontact.com/developer/docs/person/#lookup-by-email-3

0

精彩评论

暂无评论...
验证码 换一张
取 消