2017-09-28 4 views
0

URLをphpスクリプトに送信し、そのスクリプトがvarsに従って変更または作成されたメタタグを持つ同じHTMLページを返すようにします。 すべてが変更されていないタグが代わりにヘッドの本体でレンダリングされている以外の完璧な作品...ここPHPのfile_get_contentsとecho modifiedメタタグ

は私のスクリプトです:

<?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ページをロードする、いくつかのメタpropretyが存在するかどうかを確認し、それを作成していない場合は、HTMLページをエコーし​​ます。 問題は、生成された新しいHTMLでは、前のすべてのメタタグが本文にプッシュされ、頭にレンダリングされていないということです。 手掛かりがありますか? THX !!!

+0

getAttributeで何も得られない場合は、setAttributeを使用してドキュメントに作成します。 –

+0

getAttributeが完璧に機能します! str_replaceのように見えますが、この部分を削除すると古いメタタグが頭の部分に残っていますか? –

答えて

1

小さなビットのXPathを使用してmetaタグが存在するかどうかを確認する方が簡単です。しかし、主なことは、新しいタグを追加するだけでは、文書構造に配置することについて何もしないということです。 私が行ったことは、タグが存在しない場合、それらが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(); 

これは、これは1つのタグだけを行い

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

として出力を設定し、私は他の人がコードから複製するために十分に簡単になることを願っています。

+0

THX!それは動作しますが、メタが存在する場合にのみコンテンツ属性を変更するコードは何でしょうか? –

+0

コードを少し修正しました。必要なメタタグを見つけたら、属性を設定します。 –

+0

THXナイジェル、完璧に動作します! –