从内部脚本获取数据并使用php将其传递给纯文本?

时间:2017-05-29 04:03:37

标签: php json dom

基本上我想从外部HTML页面获取脚本标记内的数据,

示例:

<script type="text/javascript">
var playerInstance = jwplayer("jwplayer");
    playerInstance.setup({
    id:'jwplayer',
    width: "100%",
    height: "100%",
    aspectratio: "16:9",
    fullscreen: "true",
    primary: 'html5',
    provider: 'http',
    autostart: false,
    sources: [{"type":"video/mp4","label":360,"file":"MY-VIDEO-LINK"},{"type":"video/mp4","label":480,"file":"MY-VIDEO-LINK"}],
});

我只想要黑色标记:&gt;&gt; 来源: [{“type”:“video / mp4”,“label”:360,“file”:“MY-VIDEO-LINK”},{“type”:“video / mp4”,“label” :480, “文件”: “MY-VIDEO-LINK”}]

我可以使用PHP访问该页面:
$url = 'http://my-external-site.com/embed.php?url=blahblahblah';

我已经尝试过Curl,没有运气,还有DOM,几乎就在那里:

include_once('simple_html_dom.php');
$html = file_get_html($url);
$elem = $html->find('sources', 0); //tried with 'sources' but probably is wrong
echo $elem;

我将非常感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

尝试以下代码而不使用simple_html_dom。 EDITED

$url = 'YOUR_URL';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');
$html = curl_exec($curl);
curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($html);  //convert character asing
$xpath = new DOMXPath($dom);    
$script = $xpath->query ('//script[contains(text(),"sources:")]')->item (0)->nodeValue;

$json = end(explode( 'sources:', $script));
$json = explode ( ']', $json)[0].']';

echo $json 

希望有所帮助