Get og:image url with PHP [duplicate] - php

I'm trying to get Facebook's meta tags from my HTML.
I'm using simple html dom to get all html data from the site.
I've tried with preg_replace, but without luck.
I want for example to get the content of this fb meta tag:
<meta content="IMAGE URL" property="og:image" />
Hope someone can help! :-)

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

As per this method you will get key pair array of fabcebook open graph tags.
$url="http://fbcpictures.in";
$site_html= file_get_contents($url);
$matches=null;
preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $site_html,$matches);
$ogtags=array();
for($i=0;$i<count($matches[1]);$i++)
{
$ogtags[$matches[1][$i]]=$matches[2][$i];
}

Related

Change src atribute from img, using Simple HTML Dom php library

I'm totally new to php, and I'm having a hard time changing the src attribute of img tags.
I have a website that pulls a part of a page using Simple Html Dom php, here is the code:
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.tabuademares.com/br/bahia/morro-de-sao-paulo');
foreach($html ->find('img') as $item) {
$item->outertext = '';
}
$html->save();
$elem = $html->find('table[id=tabla_mareas]', 0);
echo $elem;
?>
This code correctly returns the part of the page I want. But when I do this the img tags comes with the src of the original page: /assets/svg/icon_name.svg
What I want to do is change the original src so that it looks like this: http://www.mywebsite.com/wp-content/themes/mytheme/assets/svg/icon_name.svg
I want to put the url of my site in front of assets / svg / icon_name.svg
I already tried some tutorials, but I could not make any work.
Could someone please kind of help a noob in php?
i could make it work. So if someone have the same question, here is how i managed to get the code working.
<?php
// Note you must download the php files simple_html_dom.php from
// this link https://sourceforge.net/projects/simplehtmldom/files/
//than include them
include_once('simple_html_dom.php');
//target the website
$html = file_get_html('http://the_target_website.com');
//loop thru all images of the html dom
foreach($html ->find('img') as $item) {
// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $item->src;
// Set a attribute
$item->src = 'http://yourwebsite.com/'.$value;
}
//save the variable
$html->save();
//findo on html the div you want to get the content
$elem = $html->find('div[id=container]', 0);
//output it using echo
echo $elem;
?>
That's it!
did you read the documentation for read and modify attributes
As per that
// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;
// Set a attribute
$e->href = 'ursitename'.$value;

How to get data or value from any div in php

i Have create php page where use many div with different id name.
so i want to get data or value from one div.
Here am showing one div with id name
i want to get data or value from this div.
<div id="tablename">tablename</div>
i have use this but its not working.
$doc = new DomDocument();
$thediv = $doc->getElementById('tablename');
echo $thediv->textContent;
So please tell me how can i get this value from my div?
You need to pass the whole content of your page to the class, otherwise, it can't select nothing since it thinks the document is empty:
$content = '<div id="tablename"></div>';
$doc = new DomDocument();
$doc->loadHTML($content); // That's the addition
$thediv = $doc->getElementById('tablename');
echo $thediv->textContent;
More info:
loadHTML(): Load the HTML from a string.
loadHTMLFile(): Load the HTML from a file.
Downloaded and include PHP Simple HTML DOM Parser from https://sourceforge.net/projects/simplehtmldom/files/ and
Try this
include 'simple_html_dom.php';
$html = file_get_html("http://www.facebook.com");
$displaybody = $html->find('div[id=blueBarDOMInspector]', 0)->plaintext;
echo $displaybody ;exit;

How to use php dom document to remove and replace tags

I have image tag <img src="path_to_file.png"> but I want that the image tag be converted to link in mobile site.
So I want img to be converted to an href:
Click here to open in new tab
I am getting started with php dom.
I could get all the attribute listed.
$newdocument = new DOMDocument();
$newdocument->loadHTML();
$getimagetag = $doc->getElementsByTagName('img');
foreach($getimagetag as $tag) {
echo $src=$tag->getAttribute('src');
}
But how do we get the src attribute , then remove the img tag completely because it contains other parameter like height and length and then create new tag of link?
Hi guys I could get it done from php dom using following code
$input="<img src='path_to_file.png' height='50'>";
$doc = new DOMDocument();
$doc->loadHTML($input);
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
$src=$tag->getAttribute('src');
$a=$doc->createElement('a','click here to open in new tab');
$a->setAttribute('href',$src);
$a->setAttribute('style','color:red;');
$tag->parentNode->replaceChild($a,$tag);
}
$input=$doc->saveHTML();
echo $input;
The create element can also be used to put text between <a></a> ie Click...new tab.
replacechild is used to remove $tag i.e. img and replace it with a tag.
By setting attribute, we can add other parameters like style,target etc.
I used php dom in the end because I only wanted the data that I get from mysql to be converted and not the other elements like logo of website. Ofcourse it can be possible using javascript too.
Thanks
#dave chen for javascript way and pointing to detecting mobile link.
#nate for pointing me to a answer.
Use phpQuery, it's amazing. It's just like using jquery! :)
https://code.google.com/p/phpquery/
I would recommend doing this with JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Images Test</title>
<script>
window.onload = changeImages;
function changeImages() {
var images = document.getElementsByTagName("img");
while (images.length > 0) {
var imageLink = document.createElement("a");
imageLink.href = images[0].src;
imageLink.innerHTML = "Click here to view " + images[0].title;
images[0].parentNode.replaceChild(imageLink, images[0]);
}
}
</script>
</head>
<body>
Here is a image of flowers : <img src="images/flowers.bmp" title="Flowers" ><br>
Here is a image of lakes : <img src="images/lakes.bmp" title="Lakes" ><br>
Here is a image of computers: <img src="images/computers.bmp" title="Computers"><br>
</body>
</html>
Example

Get Facebook meta tags with PHP

I'm trying to get Facebook's meta tags from my HTML.
I'm using simple html dom to get all html data from the site.
I've tried with preg_replace, but without luck.
I want for example to get the content of this fb meta tag:
<meta content="IMAGE URL" property="og:image" />
Hope someone can help! :-)
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
As per this method you will get key pair array of fabcebook open graph tags.
$url="http://fbcpictures.in";
$site_html= file_get_contents($url);
$matches=null;
preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $site_html,$matches);
$ogtags=array();
for($i=0;$i<count($matches[1]);$i++)
{
$ogtags[$matches[1][$i]]=$matches[2][$i];
}

Get a link and text using new simple_html_dom

Using new simple_html_dom
How I can get
The Link
The text(name)
div class="stackoverflow"
href="http://stackoverflow.com">Stackoverflow
div
I think you use inner and outertext but I'm new to all of this so I thought I'll ask the experts.
Thanks
EDIT: I removed the anchor's as they where been parsed and turned into an actually link.
From the simple_html_dom documentation: http://simplehtmldom.sourceforge.net/manual.htm
$html = str_get_html('<div class="stackoverflow" href="http://www.stackoverflow.com">Stackoverflow Div</div>');
$e = $html->find("div.stackoverflow");
$link = $e->href;
$name = $e->innertext;
Obviously you can change the html input etc.

Categories