I send a url with vars to a php script and want that script to return the same html page with modified or created meta tags according to the vars. All works perfect except the unmodified tags are rendered in the body instead of the head...
here is my script :
<?php
$imagetype = 'image/jpeg';
$panourl = $_GET["panourl"];
$snapshoturl = $_GET["snapshoturl"];
$vfw = $_GET["vfw"];
$vfh = $_GET["vfh"];
$pano_html = file_get_contents($panourl);
$html = new DOMDocument();
@$html->loadHTML($pano_html);
$meta_og_img = null;
$meta_og_img_type = null;
$meta_og_img_width = null;
$meta_og_img_height = null;
foreach($html->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('property')=='og:image'){
$meta_og_img = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:image:type'){
$meta_og_img_type = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:image:width'){
$meta_og_img_width = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:image:height'){
$meta_og_img_height = $meta->getAttribute('content');
}
}
if (is_null($meta_og_img)) {echo '<meta property="og:image" content="'.$snapshoturl.'"/>'; }
if (is_null($meta_og_img_type)) {echo '<meta property="og:image:type" content="'.$imagetype.'"/>'; }
if (is_null($meta_og_img_width)) {echo '<meta property="og:image:width" content="'.$vfw.'"/>'; }
if (is_null($meta_og_img_height)) {echo '<meta property="og:image:height" content="'.$vfh.'"/>'; }
$before = array($meta_og_img, $meta_og_img_type, $meta_og_img_width, $meta_og_img_height);
$after = array($snapshoturl, $imagetype, $vfw, $vfh);
$pano_html = str_replace($before, $after, $pano_html);
echo $pano_html->saveHTML();
?>
So I load a html page, check if some meta proprety exist and if not create it and then echo the html page back. Problem is that in the new html generated all the previous meta tags are pushed into the body and not rendered in the head... Got a clue ? THX !!!
It may be easier to use a small bit of XPath to check if the meta
tags are there. But the main thing is that just echo
ing the new tags does nothing about placing them in the document structure.
What I've done is that if the tags don't exist, they are created as a DOMelement, the attributes added and then this new tag is added to the start of the head
section.
Edit: I've updated the code to modify the meta tag if it exists, or add it if it doesn't.
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$pano_html = <<< HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta property="og:image:type" />
<title>Insert title here</title>
</head>
<body>
</body>
</html>
HTML;
$snapshoturl = "http://someurl";
$html = new DOMDocument();
libxml_use_internal_errors(true);
$html->loadHTML($pano_html);
$xp = new DOMXPath($html);
$head = $html->getElementsByTagName("head")[0];
$ogImageMeta = $xp->query("//meta[@property='og:image']");
if ( $ogImageMeta->length == 0 ) {
$newTag = $html->createElement("meta");
$newTag->setAttribute("property", "og:image");
$newTag->setAttribute("content", $snapshoturl);
$head->insertBefore($newTag,$head->firstChild);
}
else {
$ogImageMeta[0]->setAttribute("content", $snapshoturl);
}
echo $html->saveHTML();
This sets the output as
<!DOCTYPE html>
<html>
<head>
<meta property="og:image" content="http://someurl">
<meta charset="UTF-8">
<meta property="og:image:type">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
This only does the one tag, I hope that the others will be easy enough to replicate from the code.