Return user ID facebook

0

How can I get an input field informing my facebook username, returning my Facebook user id?

Case

Input: link

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

    
asked by anonymous 19.04.2017 / 01:28

1 answer

1

It depends, if you are using the official Facebook API ( due to use of the facebook-graph-api tag ) there is no way. Since API version 2.1, except for cheating, 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 id , so you can use the powerful cURL.

To get id just get the value of 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 can not 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 pass through 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 only create the sendRequest function, 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 you just need to use:

$nome = 'inkeliz';

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

    echo $id;

}

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

If you have a Facebook account, connect to it and then copy the cookies and then define 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 of a connected account, simply intercept / monitor the connection, your own is easy. Sign in to Facebook (signed in to an account) and then open Console > Network looks for a request made to facebook.com and in Request Headers copy everything that is in cookie: (yes, it's very long). This way you can get the information.

    
19.04.2017 / 03:46