Get user's name from Facebook Graph API

后端 未结 5 1696
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 16:40

I would like to know how is it possible to retrieve a string from an external page.

For example: In a PHP website, the user sends a facebook id, ex: 1157251270

相关标签:
5条回答
  • 2020-12-28 17:05

    Either you use :

    $res_json = file_gets_contents("http://graph.facebook.com/1157251270")
    $res = json_decode($res_json)
    

    Or, if you prefer curl (here with https and access token) :

    $ch4 = curl_init();
    curl_setopt($ch4, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch4, CURLOPT_URL, "https://graph.facebook.com/1157251270?access_token=YOUR_ACCESS_TOKEN");
    curl_setopt($ch4, CURLOPT_SSL_VERIFYPEER, false);
    
    if(!$result = curl_exec($ch4))
    {
        echo curl_error($ch4);
    } else {
        $res = json_decode($res_json)
    }
    
    curl_close($ch4);
    
    0 讨论(0)
  • 2020-12-28 17:15

    The Graph API returns JSON strings, so you can use:

    echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;
    

    or more verbose:

    $pageContent = file_get_contents('http://graph.facebook.com/1157251270');
    $parsedJson  = json_decode($pageContent);
    echo $parsedJson->name; // Romanos Fessas
    

    See json_decode — Decodes a JSON string

    0 讨论(0)
  • 2020-12-28 17:28

    If you are using Facebook's PHP SDK, you can also do this to query their graph API:

    $fb = new Facebook();
    $object = $fb->api('/1157251270');
    
    0 讨论(0)
  • 2020-12-28 17:29

    you get it by:

    $link = json_decode(file_get_contents('http://graph.facebook.com/1157251270'));
    echo $link->name;
    

    Nice tut: http://webhole.net/2009/08/31/how-to-read-json-data-with-php/

    0 讨论(0)
  • 2020-12-28 17:30

    For facebook data you can use json_decode.

    For another sites try with webscraping, for example: here

    0 讨论(0)
提交回复
热议问题