FacebookOpengraphInputPHP Open Graph Example

The following PHP example script demonstrates how a user might use Opengraph.io to get a website’s title, description, and icon given a URL.  This example really just shows how to get and parse the data (normal JSON)… once you have the data you can throw it on a page similar to the way that Facebook shows link information.. The OpenGraph.io API will pull down the requested URL and determine the appropriate information from the site depending on what is available.

NOTE: If you use the Compose package manager for PHP, use our Composer package! https://github.com/primeobsession/opengraph-io-php

PHP Code


  $siteUrl = 'http://cnet.com'; 
  $requestUrl = 'https://opengraph.io/api/1.1/site/' . urlencode($siteUrl); 

  // Make sure you include your free app_id here!  No CC required 
  $requestUrl = $requestUrl . '?app_id=XXXXXXXXXXXXXXXXXXXXXXXX'; 

  $siteInformationJSON = file_get_contents($requestUrl); 
  $siteInformation = json_decode($siteInformationJSON, true);
  
  print 'Title\t\t' . $siteInformation['hybridGraph']['title'] . '\n'; 
  print 'Description\t' . $siteInformation['hybridGraph']['description'] . '\n'; 
  print 'Logo URL\t' . $siteInformation['hybridGraph']['image'] . '\n'; 

To get the code above working, copy the code above into a file like test.php.  Then run the following command

php test.php

You should the output that looks like the following:

phpoutput