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 !!!
getAttributeで何も得られない場合は、setAttributeを使用してドキュメントに作成します。 –
getAttributeが完璧に機能します! str_replaceのように見えますが、この部分を削除すると古いメタタグが頭の部分に残っていますか? –