public
Last active

A single file PHP script which serves public posts to a Google Plus account as an Atom feed.

  • Download Gist
gplusatom.php
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
<?php
 
/**
* gplusatom.php
*
* A single file PHP script which serves public posts to a Google Plus account as an Atom feed.
*
* To use:
* 1) Obtain a google API key here https://code.google.com/apis/console
* 2) Fill in the 3 pieces of setup detail below.
* 3) Serve chilled
*
* Google really ought to provide this themselves in the API.
* If you agree, please go here, like the request and add some comment
* http://code.google.com/p/google-plus-platform/issues/detail?id=139
*
* @author Julian Bond <julian_bond@voidstar.com>
* @version 0.002
* @license 100% FREE TO COPY/USE FOR ANY PURPOSE, BUT DON'T BUG ME ABOUT IT, EVER.
*
* 31 October 2012
*/
 
$user_plus_id = 'CHANGE_ME'; // The ~21 digit number from your G Plus profile page
$google_api_key = 'CHANGE_ME'; // The ~40 char API key from Google
$user_full_name = 'CHANGE_ME'; // Used in the Feed Description
$max_results = 10;
$api_url = 'https://www.googleapis.com/plus/v1/people/'.$user_plus_id;
$api_url .= '/activities/public?key='.$google_api_key.'&maxResults='.$max_results;
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
 
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$info = curl_getinfo($ch);
$error = curl_error($ch);
$errno = curl_errno($ch);
 
curl_close($ch);
 
if ($code != 200) {
 
echo 'Error' . PHP_EOL;
echo $code . PHP_EOL;
print_r($response);
print_r($info);
 
} else {
 
$all = json_decode($response);
 
$title = 'Google ' . $all->title;
$updated = date(DATE_ATOM, strtotime($all->updated));
$link = 'https://plus.google.com/' . $user_plus_id . '/posts';
header('Content-type: application/atom+xml; charset=UTF-8', true);
echo '<?xml version="1.0" encoding="utf-8"?' . '>' . PHP_EOL;
echo '<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>' . PHP_EOL;
echo '<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" ';
echo ' xmlns:georss="http://www.georss.org/georss" >'."\n";
echo '<title>' . $title . '</title>'."\n";
echo '<id>' . $all->id . '</id>'."\n";
echo '<link type="text/html" rel="alternate" href="' . $link . '" />'."\n";
echo '<link type="application/atom+xml" rel="self" href="http://' . $_SERVER[SERVER_NAME].$_SERVER[REQUEST_URI] . '" />'."\n";
echo '<updated>' . $updated . '</updated>'."\n";
echo '<subtitle>Google Plus posts from ' . $user_full_name . '.</subtitle>'."\n";
 
foreach($all->items as $item) {
 
$id = $item->id;
$url = $item->url;
$published = date(DATE_ATOM, strtotime($item->published));
$updated = date(DATE_ATOM, strtotime($item->updated));
 
// Create Content
$post_content = '';
if ($item->verb == 'share' && $item->annotation) {
$post_content .= $item->annotation . "\n";
}
if ($item->verb == 'share') {
$post_content .= '<hr /><div><img src="'.$item->object->actor->image->url.'" style="padding-right:5px;float:left;" /></div>';
$post_content .= '<div><a href="' . $item->object->actor->url . '">' . $item->object->actor->displayName . '</a> originally shared this <a href="' . $item->object->url . '">post</a>:<br />';
}
if ($item->object->content != '') {
$post_content .= $item->object->content;
}
if ($item->verb == 'share') {
$post_content .= '</div><br style="clear:left" />';
}
 
if ($item->object->attachments) {
foreach ($item->object->attachments as $attachment) {
$post_content .= '<hr />';
if ($attachment->objectType == 'article') {
// get the thumbnail photo from the activity for the share
if ($item->object->attachments[1]->objectType == 'photo') {
$post_content .= '<div><a href="' . $attachment->url . '"><img src="' . $item->object->attachments[1]->image->url . '" style="padding-right:10px;float:left;" /></a></div>';
} else if ($attachment->image) {
$post_content .= '<div><a href="' . $attachment->url . '"><img src="' . $attachment->image->url . '" style="padding-right:10px;float:left;" /></a></div>';
}
 
$post_content .= '<div>';
 
//construct favicon for the source website of the share
$host = '';
if ($host = parse_url($attachment->url,PHP_URL_HOST) ) {
$post_content .= '<img src="https://s2.googleusercontent.com/s2/favicons?domain=' . urlencode($host) . '" style="padding-right:5px" />';
}
 
//Create a title and link to the shared item
$post_content .= '<a href="' . $attachment->url . '">' . $attachment->displayName . ' >></a><br />';
if ($attachment->content) {
$post_content .= $attachment->content. "\n";
}
$post_content .= '</div>';
break;
}
 
if ($attachment->objectType == 'photo') {
$post_content .= '<a href="' . $attachment->url . '"><img src="' . $attachment->image->url . '" /></a>';
}
if ($attachment->objectType == 'video') {
if ($attachment->embed->url && stristr($attachment->embed->url,'youtube.com') ) {
$post_content .= "\n" . '<iframe width="560" height="315" src="' . $attachment->embed->url . '" frameborder="0" allowfullscreen></iframe>';
} else if ($attachment->url && $attachment->image->url) {
$post_content .= "\n" . '<a href="' . $attachment->url . '"><img src="' . $attachment->image->url . '" /></a>';
} else if ($video_embed = create_video_embed($attachment->url)) {
$post_content .= "\n" . $video_embed;
}
}
}
} //Item Attachments
 
//Item Location
$georss_content = '';
if ($item->geocode) {
$coordinates = explode(' ',$item->geocode);
$gm_coordinates = implode(',',$coordinates);
$geo_cordinates = implode(' ',$coordinates);
$georss_content = " <georss:point>" . $geo_cordinates . "</georss:point>\n";
$post_content .= "\n".'<br /><a href="http://maps.google.com/?ll=' . $gm_coordinates . '&q=' . $gm_coordinates . '"><img src="http://maps.googleapis.com/maps/api/staticmap?center=' . $gm_coordinates . '&zoom=12&size=75x75&maptype=roadmap&markers=size:small|color:red|' . $gm_coordinates . '&sensor=false"></a>';
}
if ($item->placeName) {
$post_content .= "<br />\n" . $item->placeName;
}
if ($item->address) {
$post_content .= "<br />\n" . '<a href="http://maps.google.com/?ll=' . $gm_coordinates . '&q=' . $gm_coordinates . '">' . $item->address . '</a>';
}
 
//wrapping the content in CDATA can avoid some XML character set problems.
$post_content = '<![CDATA['.htmlspecialchars($post_content).']]>';
// $post_content = htmlspecialchars($post_content);
 
// Create title
$post_title = '';
$post_title = $item->title;
// If there's no title get the name of anything attached
if ( $post_title == '' && $item->object->attachments[0]->displayName ) {
$post_title = $item->object->attachments[0]->displayName;
}
// Get only the first line of the title
$post_title = explode("\n", $post_title);
$post_title = $post_title[0];
// Shorten title if it's too long
$max_characters = 255;
if (strlen($post_title)>$max_characters) {
preg_match('/(.{' . $max_characters . '}.*?)\b/', $post_title, $matches);
if ( strlen(rtrim($matches[1])) < strlen($post_title) ) {
$post_title = rtrim($matches[1]) . "...";
}
}
$post_title = htmlspecialchars($post_title);
 
$authorName = $item->actor->displayName;
$authorUrl = $item->actor->url;
$authorImageUrl = $item->actor->image->url;
 
//Output the item entry
echo '<entry>'."\n";
echo ' <title>' . $post_title . '</title>'."\n";
echo ' <content type="html">' . $post_content . ' </content>'."\n";
echo ' <id>' . $id . '</id>'."\n";
echo ' <published>' . $published . '</published>'."\n";
echo ' <updated>' . $updated . '</updated>'."\n";
echo ' <link type="text/html" rel="alternate" href="' . $url . '"/>'."\n";
echo ' <link type="image/png" rel="image" href="' . $authorImageUrl . '"/>'."\n";
echo ' <author>'."\n";
echo ' <name>' . $authorName . '</name>'."\n";
echo ' <uri>' . $authorUrl . '</uri>'."\n";
echo ' </author>'."\n";
echo $georss_content;
echo '</entry>'."\n";
 
}
 
echo '</feed>."\n"';
}
// *******************
function create_video_embed($swf_url) {
preg_match('#http://www\.youtube\.com/watch\?v=([A-Za-z0-9\-_]+)#s', $swf_url, $matches);
if (isset($matches[1])) {
$content .= '<iframe width="560" height="315" src="http://www.youtube.com/embed/'.$matches[1].'" frameborder="0" allowfullscreen></iframe>';
return $content;
} else {
preg_match('#http://w?w?w?.?vimeo.com/moogaloop.swf\?clip_id=([A-Za-z0-9\-_]+)&#s', $swf_url, $matches);
if (isset($matches[1])) {
return "http://www.vimeo.com/".$matches[1];
} else if (!isset($matches[1])) {
return null;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.