2015-02-10 33 views
0

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

我对不对?

+0

这可能是有用的,看看输入文件的内容。 – RiggsFolly 2015-02-10 17:58:58

回答

0

'og'是一个命名空间,所以它不会以这种方式拉动。你需要定义一个命名空间为您的DOMXPath对象:

http://php.net/manual/en/domxpath.registernamespace.php

编辑:这是我扔在一起使用VICE主页的例子。我从他们的开发人员网站上提取了Facebook OpenGraph XML命名空间。

<?php                    
error_reporting(E_ERROR); 
$html = file_get_contents("http://www.vice.com/"); 
$doc = new DomDocument(); 
$doc->loadHTML($html); 
$xp = new DOMXPath($doc); 
$xp->registerNamespace('og', 'http://ogp.me/ns#'); 
print_r($xp->query("//meta[@name='og:title']")->item(0)->getAttribute('content')); 
+0

我正在编辑我的问题,请看看并告诉我,如果我做对了。 – 2015-02-10 18:08:13

+0

您没有在示例页面中粘贴'og'的定义。它可能在某处......在'headertop.php'中? – haliphax 2015-02-10 18:09:20

+0

嘿托德,我编辑的问题,包括我认为我需要补充的代码,我是正确的? – 2015-02-10 18:12:35

0

这应该只是罚款:

<?php 
$html = new DOMDocument(); 
@$html->loadHTML(file_get_contents('http://www.imdb.com/title/tt0117500/')); 

foreach($html->getElementsByTagName('meta') as $meta) { 
    if(strpos($meta->getAttribute('property'), 'og') !==false) { 
     echo $meta->getAttribute('content') . '<br/>'; 
    } 
} 
?> 
+0

它没有给出任何输出 – 2015-02-10 18:27:10

+0

它当然对我来说。 – 2015-02-10 18:36:21