/
gplusrss.php
98 lines (84 loc) · 3.1 KB
/
gplusrss.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
<?php
function fetchAPI($url) {
$content = '';
if (ini_get('allow_url_fopen') == true) {
$content = file_get_contents($url);
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $key);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
}
return $content;
}
// You need a cache file(writable!)
$cacheFile = '/tmp/gplus-cache';
// Cache Time
$cacheTime = 1800;
// Google+ ID
$googlePlusID = 'XXX';
// Google+ API key
$key = 'XXX';
// https://developers.google.com/+/api/latest/activities
$url = 'https://www.googleapis.com/plus/v1/people/' . $googlePlusID . '/activities/public?alt=json&pp=1&key=';
// cache file...
$lmod = @filemtime($cacheFile);
if (!isset($_GET['purgeCache']) && $lmod && filesize($cacheFile) && (time() - $lmod < $cacheTime)) {
$content = file_get_contents($cacheFile);
} else {
$content = fetchAPI($url . $key);
if ($content)
file_put_contents($cacheFile, $content);
}
if (!$content)
die('Failed to load G+ data');
$gplus = json_decode($content);
if (!$gplus)
die('Failed to decode G+ data');
// RSS feed
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<rss version=\"2.00\" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">\n";
echo "<channel>\n";
echo " <title>" . htmlspecialchars($gplus -> title) . "</title>\n";
echo " <link>http://plus.google.com/$googlePlusID</link>\n";
echo " <description></description>\n";
echo " <pubDate>" . $gplus -> updated . "</pubDate>\n";
echo " <lastBuildDate>" . $gplus -> updated . "</lastBuildDate>\n";
foreach ($gplus->items as $item) {
$object = $item -> object;
echo " <item>\n";
echo " <title>" . htmlspecialchars($item -> title) . "</title>\n";
echo " <link>" . htmlspecialchars($item -> url) . "</link>\n";
echo " <guid>" . htmlspecialchars($item -> id) . "</guid>\n";
echo " <comments>" . htmlspecialchars($object -> replies -> selfLink) . "</comments>\n";
echo " <pubDate>" . $item -> updated . "</pubDate>\n";
if ($item -> verb == 'share') {
$source = "<a href={$object->actor->url}><img src={$object->actor->image->url} />{$object->actor->displayName}</a>";
$desc = "{$item->annotation}<p> <br/>";
$desc .= "<em>$source:</em></p><blockquote>{$object->content}</blockquote>";
} else {
$desc = $item -> object -> content;
}
if (isset($item -> object -> attachments))
foreach ($item->object->attachments as $attach) {
if ($attach -> objectType == 'article') {
$desc .= '<blockquote>';
$desc .= '<b><a href="' . $attach -> url . '">' . $attach -> displayName . '</a></b><br /><br />';
$desc .= $attach -> content;
$desc .= '</blockquote>';
} elseif ($attach -> objectType == 'photo') {
echo " <enclosure url=\"" . htmlspecialchars($attach -> image -> url) . "\" type=\"" . $attach -> image -> type . "\" />\n";
}
}
echo " <description>" . htmlspecialchars($desc) . "</description>\n";
if ($item -> geocode) {
list($lat, $lon) = explode(" ", $item -> geocode);
echo " <geo:lat>$lat</geo:lat>\n";
echo " <geo:long>$lon</geo:long>\n";
}
echo " </item>\n";
}
echo "</channel>\n";
echo "</rss>\n";