O'Reilly logo

Programming Social Applications by Jonathan LeBlanc

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Practical Implementation: Capturing Open Graph Data from a Web Source

We’ve discussed, at length, the topic of how you can make any site a producer of Open Graph information—that is, a rich provider of entity-based social data. Now that we understand that, let’s look into the process of creating an Open Graph consumer.

We will explore similar implementations of this process in two languages: PHP and Python. The end product is the same, so you can use either one you prefer.

PHP implementation: Open Graph node

First, let’s explore an Open Graph protocol parser implementation using PHP. In this example, we’ll develop a class that contains all of the functionality we need to parse Open Graph tags from any web source that contains them.

So what do want to get out of this class structure? If we break it down into a few elements, at a base level our only requirements are that it:

  • Includes a method for capturing and storing all <meta> tags with a property attribute starting with og: from a provided URL.

  • Provides one method for returning a single Open Graph tag value, and another for returning the entire list of obtained tags.

Now let’s see how these simple requirements play out when implemented in an actual PHP class structure:

<?php /******************************************************************************* * Class Name: Open Graph Parser ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required