【发布时间】:2018-09-01 16:34:04
【问题描述】:
我目前正在开展一个项目,以获取 Vimeo 视频的下载链接。如您所知,Vimeo 与 YouTube 一样,不支持通过其 REST API 直接下载功能。因为我知道 Vimeo 在 https://player.vimeo.com/video/{videoId} 的嵌入播放器的资源链接上提供了 CDN 链接,所以我创建了一个简单的 DomDocument 函数来访问包含所需下载链接的特定 JavaScript。这是我的代码:
// Define the Vimeo ID
$vimeoVideoId = htmlspecialchars((int) $_GET["id"]);
// Create new DomDocument Object.
$vimeoObject = new DOMDocument();
// Load the HTML Vimeo Embed Url
$vimeoObject->loadHTMLFile("https://player.vimeo.com/video/" . $vimeoVideoId);
// Create new XPath Dom Object
$xpathObject = new DOMXPath($vimeoObject);
// Get all JavaScript Elements without src="" attributes of the Vimeo Embed URL
Webpage
$getJavaScript = $xpathObject->query('//body//script[not(@src)]');
// Convert DomObject to Array with the help of iterator_to_array() Function.
$createArrayFromGetJavaScript = iterator_to_array($getJavaScript);
// Get rid of the first Array Part because the relevant content is in the
second Array Part
$array_shift = array_shift($createArrayFromGetJavaScript);
// Output the JavaScript Content with Vimeo CDN Video Files
echo $createArrayFromGetJavaScript[0]->nodeValue;
当您现在仔细查看生成的输出时,您会发现所有必需的信息都包含在有效的 JSON 中,并且这个有效的 JSON 是变量 r 的值。
Valid JSON inside r variable - Screenshot on what I mean
现在我的问题。上面的代码正在运行,但我怎样才能访问这个特定变量的内容?因为当我可以获得整个 JSON 元素时,我可以通过json_decode($json, true);in PHP 遍历它。有没有办法做到这一点。我想到了regex 和preg_match 或preg_match_all 之类的东西。但我还不知道如何创建这个正则表达式字符串或如何输出匹配的内容。对于初学者来说,一个好的资源也会有所帮助,因为这样我也可以为未来的项目学习一些东西。我已经在这个德国网站上进行了研究:https://www.php-einfach.de/php-tutorial/regulaere-ausdruecke/ 但在这里我只能找到 if 语句表明您正在寻找的东西在您的字符串中,但没有关于如何下载它的示例。我已经尝试过 echo preg_match(...);但这只会输出 1。所以如果你能帮我解决这个问题,那就太好了。
提前谢谢。 保罗
更新
我在这里尝试过这段代码,但现在它什么也没输出:
<?
header("Content-type: text/plain");
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Define the Vimeo ID
$vimeoVideoId = htmlspecialchars((int) $_GET["id"]);
// Create new DomDocument Object.
$vimeoObject = new DOMDocument();
// Load the HTML Vimeo Embed Url
$vimeoObject->loadHTMLFile("https://player.vimeo.com/video/" . $vimeoVideoId);
// Create new XPath Dom Object
$xpathObject = new DOMXPath($vimeoObject);
// Get all JavaScript Elements without src="" attributes of the Vimeo Embed URL Webpage
$getJavaScript = $xpathObject->query('//body//script[not(@src)]');
// Convert DomObject to Array with the help of iterator_to_array() Function.
$createArrayFromGetJavaScript = iterator_to_array($getJavaScript);
// Get rid of the first Array Part because the relevant content is in the second Array Part
$array_shift = array_shift($createArrayFromGetJavaScript);
preg_match("/var r = ({.+})/", $createArrayFromGetJavaScript[0]->nodeValue, $extracted_json);
// $extracted_json now contains the json as a string.
$json = json_decode($extracted_json[1], true);
// $json now contains the json parsed as an object.
print_r($json);
?>
相反,它显示以下错误:
Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : iframe in
https://player.vimeo.com/video/287117046, line: 1 in index.php on line 12
Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : div in
https://player.vimeo.com/video/287117046, line: 1 in index.php on line 12
Notice: Undefined offset: 1 in index.php on line 28
【问题讨论】:
标签: javascript php regex domdocument