Get Facebook meta tags with PHP
I Was going to suggest to use get_meta_tags() but it seems to not work (for me) :s
<?php
$tags = get_meta_tags('http://www.example.com/');
echo $tags['og:image'];
?>
But I would rather suggest using DOMDocument anyways:
<?php
$sites_html = file_get_contents('http://example.com');
$html = new DOMDocument();
@$html->loadHTML($sites_html);
$meta_og_img = null;
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
//If the property attribute of the meta tag is og:image
if($meta->getAttribute('property')=='og:image'){
//Assign the value from content attribute to $meta_og_img
$meta_og_img = $meta->getAttribute('content');
}
}
echo $meta_og_img;
?>
Hope it helps
How do sites put dynamic php content into facebook meta tags?
I guess the best option to switch your website to mvc
structure here is detailed article whot is mvc pattern
: http://www.sitepoint.com/the-mvc-pattern-and-php-1/
For now without major changes you can change yoru news.php
structure so that it will retrieve all content php data at the top before html part, so in that case you will have your $news
variable at all places of your html and can easily create mattes
and show content in body
Getting title and meta tags from external website
This is the way it should be:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl("http://example.com/");
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";
Get meta tags from url with DOM crawler
You need to pass HTML content to the new Crawler($html)
and not a URL.
Works fine on this page, using
viewport
, because of missingdescription
.
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">
$url = 'https://stackoverflow.com/questions/66494027/get-meta-tags-from-url-with-dom-crawler';
$html = file_get_contents($url);
$crawler = new Crawler($html);
$data = $crawler->filterXpath("//meta[@name='viewport']")->extract(['content']);
Which gives
Array
(
[0] => width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0
)
`get_meta_tags` didn't read og:image
This won't work.
get_meta_tags
uses the name
attribute of the meta tags to populate the array keys.
If you check the HTML of the page you are trying to get this values from, you'll see this:
<meta property="og:type" content= "website" />
<meta property="og:url" content="https://stackoverflow.com/"/>
<meta property="og:site_name" content="Stack Overflow" />
<meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded" />
As you can see, none of those tags have a name
attribute, so it's impossible for the function to assign those values to any key.
You will have to use a different method to get these meta tags.
Generating Facebook Open Graph meta tags dynamically
First, I want to reiterate that I am almost positive that your problem is due to the fact that the url you are passing into the debugger is not dynamically generated. The url tag essentially acts as a redirector. Unless it's the exact same (meaning the meta tags on the url meta object is the same as those on the url you are passing in) as the url you are testing, you won't get the results you're looking for.
The meta tag
<meta property="og:url">
needs to be dynamically generated. The debugger is being redirected to your default index page instead of the dynamically generated page.
For example, I assign an id to every object I'm using, and so I have something like the following
<meta property="og:url" content="http://example.com/index.php?id=<?php echo $_GET['id'] ?>"/>
I pass in that exact url into the debugger, and thus the final page the debugger lands on will be that exact url.
Also, in the following
<meta property="og:type" content=""/>
how is the property being dynamically generated? Did you remember to set in your actual code something like the following?
<meta property="og:type" content="<?php echo $_GET['type'] ?>"/>
You also appear to be shoving everything into the url, which is dangerous and can cause huge headaches, which might be the issue here. Instead, shove only one thing , eg ?type=bistro and then propagate the necessary data from the DB.
I would recommend dynamically generating most OG tags based on an object_id. Store the relevant OG info for every object_id, and then propagate them when accessed. This way, you can also easily expand and edit the tags you use when OG is updated.
If you have problems with OG you shouldn't hesitate to post them as new questions instead of comments as I guarantee other people also have the same problem.
Related Topics
How to Bind Multiple Parameters to MySQLi Query
Too Much Data with Var_Dump in Symfony2 Doctrine2
Rerouting All PHP Requests Through Index.Php
A Yaml File Cannot Contain Tabs as Indentation
Is It Secure to Store a Password in a Session
Jquery Ajax Passing Value on PHP Same Page
Split Keywords for Post PHP MySQL
Password_Hash Returns Different Value Every Time
Custom Laravel Validation Messages
How to Pull First 100 Characters of a String in PHP
Laravel 5.4 Redirection to Custom Url After Login