Main
Latest
- Google AJAX Feed API Muti Example
- Gnip API Changes
- Google Maps and Geolocation
- oEmbed, flickr and starstar
- Petition Against Public Holidays
- Good Morning
- Django Apache Configuration
- Raising funds for Autism Western Cape
- When to release the code?
- Parsing Hashtags
Archives
- June 2004
- July 2004
- August 2004
- September 2004
- October 2004
- November 2004
- December 2004
- January 2005
- February 2005
- March 2005
- April 2005
- May 2005
- June 2005
- July 2005
- August 2005
- September 2005
- October 2005
- November 2005
- December 2005
- January 2006
- February 2006
- March 2006
- April 2006
- May 2006
- June 2006
- July 2006
- August 2006
- September 2006
- November 2006
- December 2006
- January 2007
- February 2007
- March 2007
- April 2007
- May 2007
- June 2007
- July 2007
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- May 2008
- June 2008
- July 2008
- August 2008
- September 2008
- October 2008
- November 2008
- December 2008
- January 2009
Google Social Graph API and PHP 5.2
This time I have been playing with consuming the Google Social Graph API in PHP 5.2 (using the JSON extension).
<?php
$urls = array('http://charlvn.za.net/', 'http://blog.charlvn.za.net/');
$q = urlencode(implode(',', $urls));
$json = @file_get_contents("http://socialgraph.apis.google.com/lookup?q=$q&edi=1");
$data = @json_decode($json, true);
$me = array();
$other = array();
if ($data) {
foreach ($data['nodes'] as $node) {
foreach ($node['nodes_referenced_by'] as $domain => $ref) {
if (in_array('me', $ref['types'])) {
$me[] = $domain;
} else {
$other[$domain] = implode(', ', $ref['types']);
}
}
}
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Social Graph</title>
</head>
<body>
<h1>Social Graph</h1>
<?php if ($me): ?>
<h2>Me</h2>
<ul>
<?php foreach ($me as $domain): ?>
<li><a href="<?php echo htmlspecialchars($domain); ?>" rel="nofollow"><?php echo htmlspecialchars($domain); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ($other): ?>
<h2>Other</h2>
<ul>
<?php foreach ($other as $domain => $relationship): ?>
<li><a href="<?php echo htmlspecialchars($domain); ?>" rel="nofollow"><?php echo htmlspecialchars($domain); ?></a> <?php echo htmlspecialchars($relationship); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</body>
</html>
I have my social graph up as an example.
Sorry for the rel="nofollow" but I'm not going to encourage spammers.
Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.


4 Comments
Comment by
calisza on Sunday, September 14, 2008 8:22:00 PM
Very cool. It would be frightening how easy today's API's and extensions makes it to implement something like this if it wasn't so.damn.awesome.
Comment by
Charl van Niekerk on Sunday, September 14, 2008 8:41:00 PM
Yeah I think that is one of the things PHP is very good at - making things easy, especially with the SimpleXML and JSON extensions.
Comment by
developdaly on Wednesday, November 05, 2008 10:23:00 PM
Hey Charl,
I can't get this working for me. I know have PHP 5.2.6 installed. Nothing is returned though.
I'm not a programmer and I'm probably just totally missing something. All I did was copy and paste your code into a php page on my site and all that appears is the Social Graph H1.
Any advice?
Comment by
Charl van Niekerk on Tuesday, November 18, 2008 9:47:00 PM
Sorry man you'll have to give me more info. If you like feel free to drop me a mail at charlvn@charlvn.za.net sometime.
Post a Comment