首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Javascript var获取JSON数据,该变量是在PHP ()函数的帮助下创建的

问从Javascript var获取JSON数据,该变量是在PHP ()函数的帮助下创建的
EN

Stack Overflow用户
提问于 2018-09-01 16:34:04
回答 1查看 64关注 0票数 0

我目前正在进行一个项目,以获得Vimeo视频的下载链接。如您所知,Vimeo与YouTube一样,不支持通过rest直接下载功能。因为我知道Vimeo在https://player.vimeo.com/video/{videoId}的嵌入播放器的资源链接上提供CDN链接,所以我创建了一个简单的DomDocument函数来访问保存所需下载链接的特定JavaScript。这是我的密码:

代码语言: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的值。

R变量中的有效JSON -我的意思是截图

现在是我的问题。上面的代码正在按应有的方式运行,但是我如何才能访问这个特定变量的内容呢?因为当我能够获得整个JSON元素时,我可以通过PHP中的json_decode($json, true);迭代它。有没有办法。我想到了一些类似regex和preg_match或preg_match_all的东西。但我不知道如何创建这个regex字符串,也不知道如何输出匹配的内容。对于初学者来说,一个好的资源也会有所帮助,因为这样我也可以为将来的项目学到一些东西。我已经在这个德国网站上研究过了:https://www.php-einfach.de/php-tutorial/regulaere-ausdruecke/,但是在这里,我只能找到如果声明好的话,你要寻找的东西是在你的字符串中,但是没有关于如何下载它的例子。我试过了echo preg_match(.),但这只是输出1,所以如果您能帮助我解决这个问题,那就太好了。

提前结束。保罗

更新

我在这里尝试过这段代码,但现在它什么也没有输出:

代码语言:javascript
复制
<?
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);
?>

相反,它显示了以下错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-09-01 16:58:54

考虑到您对这个答案的评论,我现在了解到您希望提取json数据。

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

var r=({.+})

步骤2:使用PHP.捕获匹配

为此,我们可以使用匹配函数。

代码语言:javascript
复制
<?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.
?>

有用链接:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52130156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档