Now i solved this issue and want to share the solution in this post:
Short introduction
If you want to show your g+ posts on your website you have to use the Google+ HTTP API (programming interface to g+).
You can retrieve all public posts with the API call activities.list.
The HTTP Request to get the public posts looks like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET https://www.googleapis.com/plus/v1/people/{userId}/activities/public |
As you can see you have to put just one thing inside the url:
- the userId of your g+ account (you can find this id in the url of your g+ "profile" site)
Just enter your userId and set the collection to "public". After clicking on "execute" you can see the request to the server and the response from the server.
To get a successful response from the server you have to Authorizing your API request.
Every request that an application sends to google needs an "API Key".
How to get an API Key for the G+ API?
First you have to create a new project in the Googles API Console:
Then you have to enable the "Google+ API" Service:
![]() |
enable Google+ API Service |
![]() | |||||
accept terms of use |
![]() |
your Google+ API Service is enabled |
After the previous steps you can find you API Key at the menu item "API Access":
![]() |
your API Key |
Example:
Show Author, published date of post and the posts content on a php website:
- First download the php client library called "google-api-php-client" at the client libraries page
- After unzipping the *.gz and than the *.tar file you should see the php client library "google-api-php-client"
- create a simple "index.php" (at the same folder where you find the php client library) where you want to show your posts, it can look like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>EXAMPLE: Show g+ posts on my website</title> | |
</head> | |
<body> | |
<h1> MY G+ POSTS </h1> | |
<?php | |
include("show_posts.php"); | |
?> | |
</body> | |
</html> |
- create a "show_posts.php" and copy the following code inside the file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style type="text/css"> | |
.post-container { | |
overflow: auto; | |
border: thin solid #0F5B01; | |
margin: 0 20 20 0; | |
} | |
.post-content { | |
margin-bottom:20px; | |
} | |
.post-content-header { | |
margin-top:20px; | |
} | |
</style> | |
<?php | |
require_once 'google-api-php-client/src/Google_Client.php'; | |
require_once 'google-api-php-client/src/contrib/Google_PlusService.php'; | |
// CREATE YOUR CLIENT OBJECT: | |
$client = new Google_Client(); | |
$client->setApplicationName("Google+ PHP Starter Application"); | |
// enter your API KEY | |
$client->setDeveloperKey('enter your API Key HERE'); | |
$plus = new Google_PlusService($client); | |
$optParams = array('maxResults' => 5); | |
// enter your userId | |
$activities = $plus->activities->listActivities('enter your userId HERE', 'public', $optParams); | |
// SHOW RESULTS: | |
foreach ($activities['items'] as $activity) { | |
// WHO IS WRITING: | |
$actor = $activity['actor']; | |
$name_of_actor = $actor['displayName']; | |
// DATE | |
$published_date = $activity['published']; | |
$year = date("y", strtotime($published_date)); | |
$month = date("m", strtotime($published_date)); | |
$day = date("d", strtotime($published_date)); | |
// HEADER OF POST | |
print "<table> " . | |
"<tr> " . | |
// FIRST COLUMN --> IMAGE | |
"<td> " . | |
"<img src='{$actor['image']['url']}' />" . | |
"</td> " . | |
// SECOND COLUM | |
"<td> " . | |
"<div class='post-content-header'>" . | |
"<b> {$name_of_actor} </b> <br>" . | |
"Published: {$day}.{$month}.{$year}" . | |
"</div>" . | |
"</td> " . | |
"</tr> " . | |
"</table>"; | |
// POST CONTENT | |
$content_of_post = $activity['object']['content']; | |
// PRINT CONTENT | |
print "<div class='post-container'>" . | |
"<div class='post-content'" . | |
"{$day}.{$month}.{$year}" . | |
"<br>" . | |
"{$content_of_post}" . | |
"</div>" . | |
"</div>" ; | |
} |
- Now it's done! Open your "index.php" in a webbrowser and you see all your posts!
Keine Kommentare:
Kommentar veröffentlichen