How to get Open Graph Protocol of a webpage by php?

Asked
Active3 hr before
Viewed126 times

6 Answers

graph
90%

PHP has a simple command to get meta tags of a webpage (get_meta_tags), but this only works for meta tags with name attributes. However, Open Graph Protocol is becoming more and more popular these days. What is the easiest way to get the values of opg from a webpage. For example:, 1 As stated in the question, "this only works for meta tags with name attributes", which Open Graph metatags don't have and so is completely useless for the required purpose. – WebSmithery Aug 12 '20 at 17:57 , You're right. The approach is more suitable for a controlled environment in which you know your documents. The namespace issue could be solved by inspecting the declared namespaces, so in my opinion the bigger problem is that most HTML documents in the wild are far away from being standards compliant. – Stefan Gehrig Sep 17 '11 at 14:35 , 2 No need to start some sort of flamewar here... I actually would go for the preg_match-solution, but I just wanted to show a different and more elegant approach - which unfortunately does have some problems in the real world (most often due to the use of HTML entities or unescaped characters like <, >, & etc.) – Stefan Gehrig Sep 17 '11 at 12:48

$graph = OpenGraph::fetch('http://www.avessotv.com.br/bastidores-pantene-institute-experience-pg.html');
print_r($graph);

OpenGraph Object

(
   [_values: OpenGraph: private] => Array(
      [type] => article[video] => http: //www.avessotv.com.br/player/flowplayer/flowplayer-3.2.7.swf?config=%7B%27clip%27%3A%7B%27url%27%3A%27http%3A%2F%2Fwww.avessotv.com.br%2Fmedia%2Fprogramas%2Fpantene.flv%27%7D%7D
      [image] => /wp-content/thumbnails / 9025. jpg[site_name] => Programa Avesso - Bastidores[title] => Bastidores ⠀ œPantene Institute Experience⠀  P & G[url] => http: //www.avessotv.com.br/bastidores-pantene-institute-experience-pg.html
      [description] => Confira os bastidores do Pantene Institute Experience, da Procter & #038; Gamble. www.pantene.com.br Mais imagens:
        )

    [_position:OpenGraph:private] = > 0
   )
load more v
88%

Using https://github.com/scottmac/opengraph

Using https://github.com/scottmac/opengraph

$graph = OpenGraph::fetch('http://www.avessotv.com.br/bastidores-pantene-institute-experience-pg.html');
print_r($graph);
load more v
72%

PHP has a simple command to get meta tags of a webpage (get_meta_tags), but this only works for meta tags with name attributes. However, Open Graph Protocol is becoming more and more popular these days. What is the easiest way to get the values of opg from a webpage. For example:,With native PHP function get_meta_tags().,Unfortunately the namespace registration is needed if the HTML document uses a namespace declaration in the <html>-tag.,The basic way I see is to get the page via cURL and parse it with regex. Any idea?

PHP has a simple command to get meta tags of a webpage (get_meta_tags), but this only works for meta tags with name attributes. However, Open Graph Protocol is becoming more and more popular these days. What is the easiest way to get the values of opg from a webpage. For example:

<meta property="og:url" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:type" content="">
load more v
65%

PHP 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.,NodeJS Open Graph Example,JQuery Open Graph Example

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';
75%

It can take a lot of time to build content and maintain a website. How can we make sure our content stands out when getting shared on social feeds around the internet?,Most social networks by default will try to make their best effort in creating a preview of your content. This more often than not doesn’t go so well.,Twitter and other social media networks using open graph,og:image: This should be a link to an image that you’d like to represent your content. It should be a high resolution image that the social networks will use in their feeds.

When placing a tag on your website, you should place it in the <head> along with any other metadata. The tag used will be a <meta> tag and should look like this pattern:

<meta property=“[NAME]” content=“[VALUE]” />
load more v
40%

The Open Graph protocol enables any web page to become a rich object in a social graph. For instance, this is used on Facebook to allow any web page to have the same functionality as any other object on Facebook.,OpenGraph Ruby - Ruby Gem which parses web pages and extracts Open Graph protocol markup,PyOpenGraph - a library written in Python for parsing Open Graph protocol information from web sites,The following properties are optional for any object and are generally recommended:

As an example, the following is the Open Graph protocol markup for The Rock on IMDB:

<html prefix="og: https://ogp.me/ns#">

<head>
   <title>The Rock (1996)</title>
   <meta property="og:title" content="The Rock" />
   <meta property="og:type" content="video.movie" />
   <meta property="og:url" content="https://www.imdb.com/title/tt0117500/" />
   <meta property="og:image" content="https://ia.media-imdb.com/images/rock.jpg" />
   ...
</head>
...

</html>
load more v

Other "graph-undefined" queries related to "How to get Open Graph Protocol of a webpage by php?"