【问题标题】:Fetching "property og" meta tags from URL with PHP使用 PHP 从 URL 中获取“property og”元标记
【发布时间】:2012-03-03 16:13:28
【问题描述】:

我想创建一个类似于 Facebook 使用的发布功能(您将链接粘贴到文本框中,点击帖子,它会发布标题、描述和图像)。我意识到最好提取具有 og 属性的元标记,例如“og:title”和“og:image”,因为如果我使用普通标签,有时它们会出现换行符等其他东西,并且会出现错误.

有没有办法使用 PHP 获取这些标签的内容,但没有 AJAX 或其他自定义解析器?起点是:

<?php

$url = $_POST['link'];

?>

我们通过POST方法从上一页获取URL,剩下的怎么办?

【问题讨论】:

    标签: php url fetch facebook-opengraph meta-tags


    【解决方案1】:

    试试这个.. 它对我有用..

    foreach($linkHtml->find('head meta[property=og:url]') as $url)
    {   
        echo $url->content.'</br>';
    }
    

    【讨论】:

      【解决方案2】:

      使用这个:https://github.com/baj84/MetaData

      简单高效。

      $metaData = MetaData::fetch($url);
      var_dump($metaData->tags());
      

      【讨论】:

      • 这并不总是有效。它在 Twitter 页面上没有为我返回任何标签。
      【解决方案3】:

      我们通过 php(命令行实用程序)使用 Apache Tika 和 -j for json :

      http://tika.apache.org/

      <?php
          shell_exec( 'java -jar tika-app-1.4.jar -j http://www.guardian.co.uk/politics/2013/jul/21/tory-strategist-lynton-crosby-lobbying' );
      ?>
      

      这是来自随机监护人文章的示例输出

      {
         "Content-Encoding":"UTF-8",
         "Content-Length":205599,
         "Content-Type":"text/html; charset\u003dUTF-8",
         "DC.date.issued":"2013-07-21",
         "X-UA-Compatible":"IE\u003dEdge,chrome\u003d1",
         "application-name":"The Guardian",
         "article:author":"http://www.guardian.co.uk/profile/nicholaswatt",
         "article:modified_time":"2013-07-21T22:42:21+01:00",
         "article:published_time":"2013-07-21T22:00:03+01:00",
         "article:section":"Politics",
         "article:tag":[
            "Lynton Crosby",
            "Health policy",
            "NHS",
            "Health",
            "Healthcare industry",
            "Society",
            "Public services policy",
            "Lobbying",
            "Conservatives",
            "David Cameron",
            "Politics",
            "UK news",
            "Business"
         ],
         "content-id":"/politics/2013/jul/21/tory-strategist-lynton-crosby-lobbying",
         "dc:title":"Tory strategist Lynton Crosby in new lobbying row | Politics | The Guardian",
         "description":"Exclusive: Firm he founded, Crosby Textor, advised private healthcare providers how to exploit NHS \u0027failings\u0027",
         "fb:app_id":180444840287,
         "keywords":"Lynton Crosby,Health policy,NHS,Health,Healthcare industry,Society,Public services policy,Lobbying,Conservatives,David Cameron,Politics,UK news,Business,Politics",
         "msapplication-TileColor":"#004983",
         "msapplication-TileImage":"http://static.guim.co.uk/static/a314d63c616d4a06f5ec28ab4fa878a11a692a2a/common/images/favicons/windows_tile_144_b.png",
         "news_keywords":"Lynton Crosby,Health policy,NHS,Health,Healthcare industry,Society,Public services policy,Lobbying,Conservatives,David Cameron,Politics,UK news,Business,Politics",
         "og:description":"Exclusive: Firm he founded, Crosby Textor, advised private healthcare providers how to exploit NHS \u0027failings\u0027",
         "og:image":"https://static-secure.guim.co.uk/sys-images/Guardian/Pix/pixies/2013/7/21/1374433351329/Lynton-Crosby-008.jpg",
         "og:site_name":"the Guardian",
         "og:title":"Tory strategist Lynton Crosby in new lobbying row",
         "og:type":"article",
         "og:url":"http://www.guardian.co.uk/politics/2013/jul/21/tory-strategist-lynton-crosby-lobbying",
         "resourceName":"tory-strategist-lynton-crosby-lobbying",
         "title":"Tory strategist Lynton Crosby in new lobbying row | Politics | The Guardian",
         "twitter:app:id:googleplay":"com.guardian",
         "twitter:app:id:iphone":409128287,
         "twitter:app:name:googleplay":"The Guardian",
         "twitter:app:name:iphone":"The Guardian",
         "twitter:app:url:googleplay":"guardian://www.guardian.co.uk/politics/2013/jul/21/tory-strategist-lynton-crosby-lobbying",
         "twitter:card":"summary_large_image",
         "twitter:site":"@guardian"
      }
      

      【讨论】:

        【解决方案4】:

        解决办法是这样的:

        libxml_use_internal_errors(true);
        $c = file_get_contents("http://url/here");
        $d = new DomDocument();
        $d->loadHTML($c);
        $xp = new domxpath($d);
        foreach ($xp->query("//meta[@property='og:title']") as $el) {
            echo $el->getAttribute("content");
        }
        foreach ($xp->query("//meta[@property='og:description']") as $el) {
            echo $el->getAttribute("content");
        }
        

        【讨论】:

        • 它是 xpath,一次执行,而不是两次://meta[@property='og:title' or @property='og:description']/@content
        • 如果文档无效,则会引发异常,我会使用简单的正则表达式来获取它,而不是解析完整的文档
        • 效果很好,正是我一直在寻找的!
        【解决方案5】:

        使用类似下面的东西:

        libxml_use_internal_errors(true); // Yeah if you are so worried about using @ with warnings
        $doc = new DomDocument();
        $doc->loadHTML($html);
        $xpath = new DOMXPath($doc);
        $query = '//*/meta[starts-with(@property, \'og:\')]';
        $metas = $xpath->query($query);
        foreach ($metas as $meta) {
            $property = $meta->getAttribute('property');
            $content = $meta->getAttribute('content');
            $rmetas[$property] = $content;
        }
        var_dump($rmetas);
        

        How to get Open Graph Protocol of a webpage by php? 上找到这个 - 搜索很有帮助,Google 也一样!

        http://www.google.co.uk/search?q=meta+property+og+tags

        【讨论】:

        • 我应该把 URL 变量放在哪里? $html 还是 $doc?
        • 我应该认为 $html ;) $doc 是 OOP
        • 它不断地给我“未定义的变量:C:\xampp\htdocs\linkedit\index.php 中的 rmetas on line 73 NULL” 第 73 行是 var_dump($rmetas);
        • 没关系,找到了,最好的方法与您发布的方法类似,可以在 Artefacto 的帖子(第 4 个答案)下的 link 找到,唯一的事情是您必须将 name='keywords' 更改为 property='og:title' 并且效果很好
        • 如果有人偶然发现您的问题,请随时勾选接受答案按钮或发布您的解决方案作为答案以供将来参考:)
        猜你喜欢
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 2014-07-16
        相关资源
        最近更新 更多