PHP file_get_contents 和 echo 修改的元标记

PHP file_get_contents and echo modified meta tags

我将带有 vars 的 url 发送到 php 脚本,并希望该脚本发送到 return 相同的 html 页面,并根据变种。 除了未修改的标签呈现在 body 而不是 head...

之外,所有作品都很完美

这是我的脚本:

<?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();
?>

所以我加载了一个 html 页面,检查是否存在某些元属性,如果不存在则创建它,然后返回 html 页面。 问题是在新的 html 中生成的所有以前的元标记都被推入 body 而不是在头部呈现...... 有线索吗? 谢谢 !!!

使用少量 XPath 检查 meta 标签是否存在可能更容易。但最主要的是 echoing 新标签对将它们放置在文档结构中没有任何作用。 我所做的是,如果标签不存在,它们将创建为 DOMelement,添加属性,然后将这个新标签添加到 head 部分的开头。

编辑:我已更新代码以修改元标记(如果存在)或添加它(如果不存在)。

<?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();

这会将输出设置为

<!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>

这只做一个标签,我希望其他标签很容易从代码中复制。