【问题标题】:Get Vine video url and image using PHP simple HTML DOM Parser使用 PHP 简单的 HTML DOM Parser 获取 Vine 视频 url 和图像
【发布时间】:2013-07-09 18:30:44
【问题描述】:

所以我喜欢使用 PHP Simple HTML DOM Parser 获取 vine 图像 url 和视频 url。

http://simplehtmldom.sourceforge.net/

这是一个示例藤网址

https://vine.co/v/bjHh0zHdgZT

所以我需要从 URL 中获取此信息。表单图片网址:

<meta property="twitter:image" content="https://v.cdn.vine.co/v/thumbs/8B474922-0D0E-49AD-B237-6ED46CE85E8A-118-000000FFCD48A9C5_1.0.6.mp4.jpg?versionId=mpa1lJy2aylTIEljLGX63RFgpSR5KYNg">

对于视频网址

<meta property="twitter:player:stream" content="https://v.cdn.vine.co/v/videos/8B474922-0D0E-49AD-B237-6ED46CE85E8A-118-000000FFCD48A9C5_1.0.6.mp4?versionId=ul2ljhBV28TB1dUvAWKgc6VH0fmv8QCP">

我只想获取这些元标记的内容。如果有人可以帮助真的很感激。谢谢

【问题讨论】:

  • “我只想获取这些元标记的内容”。你能准确地表达你的意思吗(也许有一点“破代码”):“echo myFunction($theVideoURL); 应该会导致https://v.cdn.vin.co/...”?你试过什么?

标签: php php-5.3 simple-html-dom


【解决方案1】:

在这个例子中我使用的是原生 PHP DOM,而不是你指出的 lib,它应该可以工作。

这是我为类似的东西创建的一个小类:

<?php

class DomFinder {
  function __construct($page) {
    $html = @file_get_contents($page);
    $doc = new DOMDocument();
    $this->xpath = null;
    if ($html) {
      $doc->preserveWhiteSpace = true;
      $doc->resolveExternals = true;
      @$doc->loadHTML($html);
      $this->xpath = new DOMXPath($doc);
      $this->xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");
    }
  }

  function find($criteria = NULL, $getAttr = FALSE) {
    if ($criteria && $this->xpath) {
      $entries = $this->xpath->query($criteria);
      $results = array();
      foreach ($entries as $entry) {
        if (!$getAttr) {
          $results[] = $entry->nodeValue;
        } else {
          $results[] = $entry->getAttribute($getAttr);
        }
      }
      return $results;
    }
    return NULL;
  }

  function count($criteria = NULL) {
    $items = 0;
    if ($criteria && $this->xpath) {
      $entries = $this->xpath->query($criteria);
      foreach ($entries as $entry) {
        $items++;
      }
    }
    return $items;
  }

}

要使用它,您可以尝试:

$url = "https://vine.co/v/bjHh0zHdgZT";
$dom = new DomFinder($url);
$content_cell = $dom->find("//meta[@property='twitter:player:stream']", 'content');
print $content_cell[0];

【讨论】:

  • 你用这个代码和url测试过吗?请注意,我使用了您提供的 url 并使用 property='twitter:player:stream 捕获了视频,如果您使用不同的 url,也许您应该更改其他属性。
  • 我在几分钟前用这个代码测试了它,使用命令行 php 版本 5.3.26,但曾经使用 php 5.2 做同样的事情。
猜你喜欢
  • 2016-04-21
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 2012-04-08
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
相关资源
最近更新 更多