简体   繁体   English

PHP file_get_contents和回显修改的元标记

[英]PHP file_get_contents and echo modified meta tags

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. 我将带有vars的网址发送到php脚本,并希望该脚本根据vars返回具有已修改或创建的meta标签的相同html页面。 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. 因此,我加载了一个html页面,检查是否存在一些元属性,如果不存在,则将其回显。 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 ? 问题是,在生成的新html中,所有以前的meta标签都被推到了正文中,而不是呈现在头部中。。。 THX !!! 谢谢 !!!

It may be easier to use a small bit of XPath to check if the meta tags are there. 使用少量的XPath来检查meta标记是否存在可能会更容易。 But the main thing is that just echo ing the new tags does nothing about placing them in the document structure. 但是最主要的是,仅echo显新标记对将它们放置在文档结构中没有任何作用。 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. 我所做的是,如果标签不存在,则将它们创建为DOMelement,添加属性,然后将此新标签添加到head

Edit: I've updated the code to modify the meta tag if it exists, or add it if it doesn't. 编辑:我已经更新了代码,以修改meta标签(如果存在),或者添加它(如果不存在)。

<?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. 这只做一个标签,我希望其他标签足够容易从代码中复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM