Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

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.

4 Comments

Comment by Blogger 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 Blogger 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 Blogger 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 Blogger 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

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.