【问题标题】:Parse from javascript var with SimpleHTMLDom使用 SimpleHTMLDom 从 javascript var 解析
【发布时间】:2018-09-17 11:00:30
【问题描述】:

我有这段代码可以用 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】:

    其他人建议 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'];
    

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      相关资源
      最近更新 更多