Return facebook user ID

Asked

Viewed 520 times

0

As I can through an input field informing my facebook username, return my Facebook user number (id)?

Case

Input: http://www.facebook.com/usuario

Output: $profile= '4559875758';echo $profile;

1 answer

1


Depends, if you’re using the official Facebook API (due to the use of the facebook-Graph-api tag) there is no way. Since version 2.1 of the API, unless mistaken, the identifiers are individual, so each user (and each application) has a id different for the same user.


However, if you are not using the Official API, then you can simply make a request to https://facebook.com.br/seu_nome and get the id, for this you can use the powerful Curl.

To obtain the id just get the value of entity_id which is present on the profile page, in the HTML code. For this purpose we use REGEX (/"entity_id":([0-9]+)/) to simplify the matter:

function getFacebookIdentifier($ProfileContent_result){

    if(preg_match('/"entity_id":([0-9]+)/', $ProfileContent_result, $matches)){
        return $matches[1];
    }

    return false;

}

This will return false if it is not possible to find.

To get the profile content, the page, we will use CURL, however basically we need two things:

  • URL to connect (https://m.facebook.com/nome_de_usuario).
  • User-Agent to pass as a common browser (in this case Blackberry 10, after all we are connecting with the https://m.)

In the end you’ll have this:

function getFacebookProfileContent($name){

    return sendRequest('https://m.facebook.com/'.$name, [
        'User-Agent: Mozilla/5.0 (BB10; Kbd) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.1.0.4633 Mobile Safari/537.10+'
    ]);

}

Now we just create the function sendRequest, who will be tasked to make the call using CURL:

function sendRequest($url, $headers = []){

    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_FAILONERROR => 1,
        CURLOPT_RETURNTRANSFER => 1
    ]);

    return curl_exec($ch);

}

/!\ This URL has a number of security issues, be aware of this!


So just use:

$nome = 'inkeliz';

if($id = getFacebookIdentifier( getFacebookProfileContent($nome) )){

    echo $id;

}

A solution for blocking "not be found by the search system" is to specify the cookies of an already connected Facebook account, so Facebook will get the profile because it is connected to Facebook (and not a visitor).

If you have a Facebook account, connect to it and then copy the cookies and then set using:

    sendRequest('https://m.facebook.com/'.$name, [
        'User-Agent: Mozilla/5.0 (BB10; Kbd) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.1.0.4633 Mobile Safari/537.10+',
        'cookie: '.$_SEU_COOKIE_AQUI
    ]);

To obtain the cookie from a connected account, just intercept/monitor the connection, your own is easy. Sign in to Facebook (logged in to an account) and then open Console > Network search for a request made to the facebook.com and in Request Headers copy everything in cookie: (yes, it’s quite long). This way you can get the information.

  • Which error presented? Is there something in the logs? Do you have Curl enabled? This is PHP, it must be processed on your server, so it doesn’t matter if it is in an iframe or outside it. To manipulate a iframe both must be in the same field.

  • I just did a test, with the same code (https://pastebin.com/nnZRGzVi) using XAMPP (with PHP 7.1) and the result was the id of the user, usually.

  • The curious thing is that for some profiles it works, for others it does not bring value. Try for Davi.reimannrossini

  • Returned normally, 749748573, that is correct.

  • This is a configuration of your Facebook account, at https://www.facebook.com/settings?tab=privacy&section=search&view, enable your profile to be visible to those without an account. There is a solution to this, I will add that information. Try to access this URL through anonymous browser, no login with Facebook, this is the same as Curl gets, which is not your profile. ;)

  • Now it made sense why it wouldn’t show up. But what if it’s a user that isn’t me logged in? I would have to capture/intercept the cookie and call it on the variable. Correct?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.