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 ...

Get Programming Social Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.