這裏是我使用在這一點上的代碼無法從網頁中提取og標籤?
$file = array_rand($files);
$filename = "http://example.com/".$files[$file];
echo $filename;
libxml_use_internal_errors(true);
$c = file_get_contents($filename);
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
foreach ($xp->query("//meta[@name='og:title']") as $el) {
echo $el->getAttribute("content");
}
foreach ($xp->query("//meta[@name='og:image']") as $el) {
echo $el->getAttribute("content");
}
$ filename的URL的正確值,但它並沒有呼應OG的內容:圖片和og:標題?
編輯
這是我的網頁的典型組織
<?php require_once("headertop.php")?>
<meta property="og:image" content="url" />
<meta property="og:title" content="content here." />
<meta property="og:description" content="description here." />
<title>Page title</title>
<?php require_once("headerbottom.php")?>
EDIT 2
From one answer I understood this. I have to use
$rootNamespace = $d->lookupNamespaceUri($d->namespaceURI);
$xpath->registerNamespace('og', $rootNamespace);
然後用
<meta property="og:image" content="url" />
我對不對?
這可能是有用的,看看輸入文件的內容。 – RiggsFolly 2015-02-10 17:58:58