【问题标题】:what is the best way to get og:image meta property [duplicate]获取 og:image 元属性的最佳方法是什么
【发布时间】:2019-01-07 02:22:05
【问题描述】:

我正在尝试在我的网站中显示 rss 提要链接,一切顺利,但使用 file_get_contents() 方法获取 og:image 属性需要花费大量时间。还有其他方法可以获取元标记属性吗?

Python 是否有助于更快地获取这些标签?

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    这就是我以前获取所有 og:tags 的方式:

    libxml_use_internal_errors(true);
    $doc = new DomDocument();
    $doc->loadHTML(file_get_contents($url));
    $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');
        echo '<h1>Meta '.$property.' <span>'.$content.'</span></h1>';
    }
    

    【讨论】:

    • 看不到$queryT = '';的用途,如果没有用,应该放弃。
    • 如您所知,这是我以前使用的代码的复制粘贴。但是同意,我删除了它;)
    【解决方案2】:
    <?php
    $page_content = file_get_contents('http://example.com');
    
    $dom_obj = new DOMDocument();
    $dom_obj->loadHTML($page_content);
    $meta_val = null;
    
    foreach($dom_obj->getElementsByTagName('meta') as $meta) {
    
    if($meta->getAttribute('property')=='og:image'){ 
    
        $meta_val = $meta->getAttribute('content');
    }
    }
    echo $meta_val;
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2018-03-24
      • 1970-01-01
      • 2020-11-26
      • 2013-11-12
      • 1970-01-01
      相关资源
      最近更新 更多