Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件获取内容并回显修改后的元标记_Php_Domdocument_File Get Contents_Meta Tags - Fatal编程技术网

PHP文件获取内容并回显修改后的元标记

PHP文件获取内容并回显修改后的元标记,php,domdocument,file-get-contents,meta-tags,Php,Domdocument,File Get Contents,Meta Tags,我向php脚本发送一个带有vars的url,并希望该脚本返回相同的html页面,其中包含根据vars修改或创建的元标记。 除了未修改的标记在主体中而不是在头部中呈现外,所有工作都是完美的 这是我的剧本: <?php $imagetype = 'image/jpeg'; $panourl = $_GET["panourl"]; $snapshoturl = $_GET["snapshoturl"]; $vfw = $_GET["vfw"]; $vfh = $_GET["vfh"]; $pan

我向php脚本发送一个带有vars的url,并希望该脚本返回相同的html页面,其中包含根据vars修改或创建的元标记。 除了未修改的标记在主体中而不是在头部中呈现外,所有工作都是完美的

这是我的剧本:

<?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中,所有以前的元标记都被推入主体中,而不是呈现在头部。。。 有线索吗?
谢谢

使用一点XPath检查元标记是否存在可能更容易。但最重要的是,仅仅回显新标记并不能将它们放置在文档结构中。 我所做的是,如果标记不存在,它们将被创建为一个domeElement,添加属性,然后将这个新标记添加到head部分的开头

编辑:我已经更新了代码来修改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();
这会将输出设置为

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

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

如果getAttribute没有提供任何内容,那么使用setAttribute在文档中创建它。getAttribute工作得很好!看起来问题出在str_replace上,如果我删除了这个部分,旧的元标记会留在头部部分???THX!这是可行的,但是如果元存在,那么只修改内容属性的代码是什么呢?我稍微修改了代码,如果它找到了你想要的元标记,它就会设置属性。抱歉,奈杰尔,还有一个问题。。。如果在加载的html中有一个meta标记以这种方式关闭,则我有一个错误:警告:DOMDocument::loadHTML:意外的结束标记:实体中的meta,。。。有没有办法避免这个错误?谢谢!如果添加对libxml的调用,请使用\u internal\u errorstrue;-应该压制对奈杰尔造成恶作剧的警告!现在我在另一台服务器上遇到了另一个问题,我想是不同的php版本吧致命错误:无法将DOMNodeList类型的对象用作$ogUrlMeta[0]->setAttributecontent;很抱歉这么新手。。。