简体   繁体   中英

How to get facebook page id via php?

Suppose i have a facebook page link http://facebook.com/page_name and i am not the admin of the page. Is it possible to find out the facebook id, profile pics of the page and fan count?

Thanks!

FB id and some informations: yes. See Graph API docs .

edit: Some code to get id from my app:

$arg = $_REQUEST['arg'];
if ($arg != ''){

        $arg = str_replace(
                array('http://','www.','facebook.com/','/'),
                '',
                $arg
        );

        $elseID = file_get_contents('https://graph.facebook.com/?ids='.$arg.'&fields=id');
        $elseID = json_decode($elseID,true);
        $elseID = $elseID[$arg]['id'];
}

Typically you can do http://graph.facebook.com/ {page_name} and receive the appropriate results. For example, Pepsi is at http://www.facebook.com/pepsi and you can see the graph API results at https://graph.facebook.com/pepsi .

You can use Facebook SDK for PHP https://github.com/facebook/php-graph-sdk

Below my solution

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

$pageUrl = 'some_url';  
      FacebookSession::setDefaultApplication('facebookAppId','facebookAppSecret');
    //getting session
    $Session = FacebookSession::newAppSession();
    try
    {
      //validating if session is correct
      $Session->validate();
      //sending request
      $Request = new FacebookRequest($Session, 'GET', '/',
                                      array (
                                        'id' => $pageUrl,
                                      )
                                    );
      $Response = $Request->execute();
      //getting UserGraph object
      $UserObject = $Response->getGraphObject(GraphUser::className());
      //return page id
      return $UserObject->GetId();
    }
    catch (FacebookRequestException $ex)
    {}
    catch (Exception $ex)
    {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM