Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Facebook Profile image from email?

Tags:

facebook

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 email 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.

like image 445
Forbini Avatar asked May 17 '10 18:05

Forbini


2 Answers

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

like image 174
Sergey Kornilov Avatar answered Dec 11 '22 18:12

Sergey Kornilov


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 = '[email protected]';

/* 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 . '" />';

        }

    }

}


?
like image 38
McHerbie Avatar answered Dec 11 '22 18:12

McHerbie