1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to get Facebook share count for url

Discussion in 'PHP' started by abyse, Jun 11, 2017.

  1. #1
    I used this script to grab google plus and facebook shares:

    <?
    class share {
    private $url,$timeout;
    function __construct($url,$timeout=10) {
    $this->url=rawurlencode($url);
    $this->timeout=$timeout;
    }
    function get_fb() {
    $json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$url);
    $json = json_decode($json_string, true);
    return isset($json[0]['share_count'])?intval($json[0]['share_count']):0;
    }
    function get_gp()  {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($this->url).'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    $curl_results = curl_exec ($curl);
    curl_close ($curl);
    $json = json_decode($curl_results, true);
    return isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['count'] ):0;
    }
    private function file_get_contents_curl($url){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
    $cont = curl_exec($ch);
    if(curl_error($ch))
    {
    die(curl_error($ch));
    }
    return $cont;
    }
    }
    ?>
    PHP:

    In header.php i had this:
    <? echo $obj->get_fb(); ?>
    PHP:
    to display facebook share count for my page/url but now I get this error from facebook: (
    REST API is deprecated for versions v2.1 and higher) is there any way to get facebook share count from: http://graph.facebook.com/?id=http://www.mysite.com ?
    SEMrush
     
    abyse, Jun 11, 2017 IP
    SEMrush