Skip to content

Instantly share code, notes, and snippets.

@davidlevy
Last active December 18, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidlevy/5747450 to your computer and use it in GitHub Desktop.
Save davidlevy/5747450 to your computer and use it in GitHub Desktop.
Get values for Schema.org interactionCount properties
/*
Example :
<?php $url = 'http' . ($_SERVER['HTTPS'] ? 's':'') .'://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; ?>
<meta itemprop="interactionCount" content="UserTweets:<?php echo get_tweets($url);?>"/>
*/
function get_tweets($url) {
$url = urlencode($url);
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
function get_likes($url) {
$url = urlencode($url);
$json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url);
$json = json_decode($json_string, true);
return intval( $json[$url]['shares'] );
}
function get_plusones($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $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 intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment