Monday, August 10, 2015

Wordpress- get facebook like count by url

Wordpress-  get facebook like count by url

$link   = get_permalink( $post->ID );
// make the call
$call   = 'https://graph.facebook.com/fql?q=SELECT%20like_count%20FROM%20link_stat%20WHERE%20url=%27' . urlencode( $link ) . '%27';
// do the call
$rmget  = wp_remote_get( $call, array( 'sslverify' => false ) );
// error. bail.
if( is_wp_error( $rmget ) ) {
    return false;
}
// parse return values
$return = json_decode( $rmget['body'] );
// bail with no decoded
if ( empty( $return ) || empty( $return->data ) || empty( $return->data[0] ) || empty( $return->data[0]->like_count ) ) {
    return false;
}

// get the count
echo 'CONT is ', $count  = $return->data[0]->like_count;