-
Notifications
You must be signed in to change notification settings - Fork 4
/
SocialCount.php
145 lines (127 loc) · 3.26 KB
/
SocialCount.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
interface SocialNetwork
{
public function getKey();
public function getShareCount($url);
}
class Twitter implements SocialNetwork
{
public function getKey()
{
return 'twitter';
}
public function getShareCount($url)
{
$contents = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
if($contents) {
return json_decode($contents)->count;
} else {
return NULL;
}
}
}
class Facebook implements SocialNetwork {
public function getKey()
{
return 'facebook';
}
public function getShareCount($url)
{
$contents = file_get_contents('http://graph.facebook.com/?id=' . $url);
if($contents) {
$json = json_decode($contents);
return isset($json->shares) ? $json->shares : 0;
} else {
return NULL;
}
}
}
class GooglePlus implements SocialNetwork
{
public function getKey()
{
return 'googleplus';
}
public function getShareCount($url)
{
// Warning! Reverse Engineered, not an Actual API
// http://johndyer.name/getting-counts-for-twitter-links-facebook-likesshares-and-google-1-plusones-in-c-or-php/
// Open use license per https://twitter.com/johndyer/status/223239624498229248
$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);
if($curl_results) {
$json = json_decode($curl_results, true);
if(!isset($json[0]['error'])) {
return $json[0]['result']['metadata']['globalCounts']['count'];
} else {
return NULL;
}
} else {
return NULL;
}
}
}
class ShareThis implements SocialNetwork {
const PUB_KEY = 'a3cce920-3a6b-47a8-a890-d27d55cbc9e8';
const ACCESS_KEY = '512db7bf2cce2acb63fad31b31067e27';
public function getKey()
{
return 'sharethis';
}
public function getShareCount($url)
{
$contents = file_get_contents('http://rest.sharethis.com/reach/getUrlInfo.php?url=' . $url . '&pub_key=' . self::PUB_KEY . '&access_key=' . self::ACCESS_KEY);
if($contents) {
$json = json_decode($contents);
return $json->total->inbound;
} else {
return NULL;
}
}
}
/*
* SocialCount
* Returns share, like, and comment counts for various popular social networks in a single ajax request.
*
* Usage:
* service.php?url=http://www.google.com/
*/
class SocialCount
{
private $url,
$services = array();
const EMPTY_RESULT = '""';
function __construct($url)
{
if(empty($url)) {
throw new Exception('"url" required.');
}
$this->url = $url;
}
public function addNetwork(SocialNetwork $network)
{
$this->services[] = $network;
}
public function toJSON() {
$services = array();
foreach($this->services as $service) {
$count = $service->getShareCount($this->url);
$services[] = '"' . $service->getKey() . '": ' . (is_null($count) ? self::EMPTY_RESULT : $count);
}
return '{' . implode(',', $services) . '}';
}
}