使用SimpleHTMLDom从javascript var解析

问题描述 投票:0回答:1

我有这个代码,用curl输出源URL的源页面!

$url = 'http://source-page.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // add this one, it seems to spawn redirect 301 header
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); // spoof
$output = curl_exec($ch);
curl_close($ch);

$html = str_get_html($output);

在$ output我有这个:

var flashvars = {

    "image_url":"http://path-to-image.com",
    "video_title":"This is video title",
    "videoUrl":"http://this-is-path-to-mp4.com"

}

我想回应videoUrl,我试过这个:

$videoUrl = $html->find('flashvars[0].videoUrl');
echo $videoUrl

并且给我空洞的结果。这样做的好代码是什么?

javascript php html curl simple-html-dom
1个回答
0
投票

其他人建议使用regex + json_decode然后将其删除。这就是我要做的事情:

$output = <<<EOF
var flashvars = {

  "image_url":"http://path-to-image.com",
  "video_title":"This is video title",
  "videoUrl":"http://this-is-path-to-mp4.com"

}
EOF;

$str = preg_match('/var flashvars = (\{.*?\})/s', $output, $m);
$data = json_decode($m[1], true);
echo $data['videoUrl'];
© www.soinside.com 2019 - 2024. All rights reserved.