Get User Id from Facebook in PHP

前端 未结 2 502
耶瑟儿~
耶瑟儿~ 2020-12-29 17:19

I am trying to make a Facebook app and need user id from Facebook when the user opens the applicaiton. I have setup my application and its show mock form on the Facebook can

2条回答
  •  攒了一身酷
    2020-12-29 17:43

    Here is a sort of hack code written by me which allows to fetch any facebook user id even if he is not logged in or authorized by app https://github.com/invisiblevision/get-facebook-id/

    "GET",        //set request type post or get
                CURLOPT_POST           =>false,        //set to GET
                CURLOPT_USERAGENT      => $user_agent, //set user agent
                CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
                CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
                CURLOPT_RETURNTRANSFER => true,     // return web page
                CURLOPT_HEADER         => false,    // don't return headers
                CURLOPT_FOLLOWLOCATION => true,     // follow redirects
                CURLOPT_ENCODING       => "",       // handle all encodings
                CURLOPT_AUTOREFERER    => true,     // set referer on redirect
                CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
                CURLOPT_TIMEOUT        => 120,      // timeout on response
                CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            );
            $ch      = curl_init( $url );
            curl_setopt_array( $ch, $options );
            $content = curl_exec( $ch );
            $err     = curl_errno( $ch );
            $errmsg  = curl_error( $ch );
            $header  = curl_getinfo( $ch );
            curl_close( $ch );
            $header['errno']   = $err;
            $header['errmsg']  = $errmsg;
            $header['content'] = $content;
            return $header;
        }
    
    /*Getting user id */
    $url = 'http://findmyfbid.com';
    $data = array('url' => $profile_url );
    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    function getData($data)
    {
        $dom = new DOMDocument;
        $dom -> loadHTML( $data );
        $divs = $dom -> getElementsByTagName('code');
        foreach ( $divs as $div )
        {
                return $div -> nodeValue;
    
        }
    }
    $uid = getData($result);  // User ID
    

提交回复
热议问题