【问题标题】:Get JSON Data from Javascript var that was created with the help of PHP DomDocument() Function从使用 PHP DomDocument() 函数创建的 Javascript var 获取 JSON 数据
【发布时间】: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 遍历它。有没有办法做到这一点。我想到了regexpreg_matchpreg_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


    【解决方案1】:

    考虑到您对此答案的 cmets,我现在了解到您希望提取 json 数据。

    第 1 步:构造正则表达式以提取 json 数据。 由于您提到 json 存储在 r 变量中,并且鉴于 json 以 {-bracket 开头并以 }-bracket 结尾,我们可以使用以下正则表达式来捕获两者之间的所有内容,因为我们知道正则表达式匹配第一个最长的匹配项。

    var r=({.+})

    第 2 步:使用 PHP 捕获匹配项。

    我们可以为此使用preg_match 函数。

    <?php
    $inputstring = 'function() var r = {"cdn_url": ..", "request": {"files": {"dash": { "id": 654654654, "fps": 23}}}}';
    preg_match("/var r=({.+})/", $inputstring, $extracted_json);
    // $extracted_json now contains the json as a string.
    $json = json_decode($extracted_json[1]);
    // $json now contains the json parsed as an object.
    ?>
    

    有用的链接:

    【讨论】:

    • 非常感谢您的回答。上面的链接非常有用。他们会在做一些正则表达式实验时帮助我。 :-) 现在回答你。我可能还不够准确地说出我的意思。我在这里有这个输出:textsaver.flap.tv/lists/24w3。我需要一个解决方案来获取第一个 var {r} 中的值,因为它是有效的 JSON,我可以简单地通过 json_decode(); 解析它。我当前的正则表达式是这样的: (var r = {|}$) 是否可以获取 JavaScript var {r} 的内容?我不知道这是否是一个正确的开始。我也测试了你的代码,但它什么也没输出。
    • 有可能是的,我会在几秒钟内更新我的答案。
    • 非常感谢@thepieterdc
    • 感谢您的回答。但我认为问题有点复杂。这是我的 JSFiddle -> jsfiddle.net/u7d14bat 这是我上面提到的函数输出的确切代码。但是,当我测试您的代码时,它还会使用 JSON 输出一点 JavaScript,当我使用完整的 JavaScript 输入对其进行测试时,它会生成此处显示的错误:3v4l.org/1OmAl(警告:json_decode() 期望参数 1 为字符串, array given ...) 我实际上不知道如何解决这个问题,因为当我尝试输出 print_r($extracted_json) 它是一个空数组。
    • 也许问题是在我的 JavaScript 输入中,代码中还有多个关闭 }。也许这就是您的函数也包含它们的原因。我还看到 var = r 在 $extracted_json 变量中仍然可用。嗯很复杂。希望我们能解决这个问题,因为目前网络上没有太多 vimeo php 下载器替代品。
    猜你喜欢
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多