这里是我使用在这一点上的代码无法从网页中提取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