我如何使用PHP获得与Facebook相同的墙贴

时间:2014-04-19 03:57:39

标签: php facebook curl domdocument file-get-contents

也许这不是正确的问题,但我正在寻求帮助,我使用curl,DOMDocument和file_get_contents来获取图像标记为facebook,但我不能。 enter image description here

有点像这样。

2 个答案:

答案 0 :(得分:1)

Facebook使用特定的Open Graph元标记,发布者可以使用它来建议Facebook在共享URL时使用的图像,标题和描述。您将需要使用DOMDocument加载HTML,然后查找这些标记。

在名称中查找标有og:的标记,如下所示:

<meta property="og:image" content="http://example.com/image.png" />

另请参阅:http://davidwalsh.name/facebook-meta-tags

答案 1 :(得分:0)

我的代码如下所示: 我正试图从测试中获取来自雅虎链接的图像 https://es.finance.yahoo.com/noticias/amazon-prev-vender-un-smartphone-200856947.html

header('Content-Type: application/json');
if(isset($_GET['c_txt'])){
$url = $_GET['c_txt'];
if(curl_init($url)){
    $html = file_get_contents($url);

    $doc = new DOMDocument();
    @$doc->loadHTML($html);

    $tags = $doc->getElementsByTagName('img');

    foreach ($tags as $tag) {
           echo $tag->getAttribute('src') . "\n";
    }
  }
}

但结果如下:

https://sb.scorecardresearch.com/b?  c1=2&c2=7241469&c7=es.finance.yahoo.com%2Fnoticias%2Famazon-prev-vender-un-smartphone-200856947.html&c5=1185584786&c15=$&cv=2.0&cj=1
https://s.yimg.com/dh/ap/default/130307/expansion_header_article.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/dh/ap/default/130711/masnoticiasen2.jpg
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png
https://s.yimg.com/os/mit/media/m/base/images/transparent-1093278.png

而不是在facebook上显示的图片,我想要的标签就是这个: enter image description here