Return facebook user ID

Facebook Facebook username

How do I get 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 Prof profile;

Author: Davi R Rossini, 2017-04-18

1 answers

Facebook Facebook API (due to the use of the Facebook-graph-apitag) there is no way. Since API version 2.1, barring error, the identifiers are individual, so each user (and each application) has a different id for the same user.


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

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

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 cannot be found.

To get the content of the profile, 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 impersonate a common browser (in this case BlackBerry 10, after all we are connecting with https://m.)

At the end you will 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, which will be in charge of making 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 cURL has a number of security issues, be aware of this!


That way is enough use:

$nome = 'inkeliz';

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

    echo $id;

}

{Facebook facebook facebook

A solution to blocking"not to 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 logged in on 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 get the cookie from a connected account, simply intercept / monitor the connection, your own is easy. Log in to Facebook (logged in to an account) and then open Console > Network look for a request made for facebook.com and in Request Headers copy everything that is in cookie: (yes, it is quite long). This way you can get the information.

 1
Author: Inkeliz, 2017-04-20 04:50:13