【问题标题】:Load HTML Source to String in PHP在 PHP 中将 HTML 源代码加载到字符串
【发布时间】:2016-01-20 21:25:22
【问题描述】:

我正在尝试将远程页面的 HTML 源代码加载到 PHP 中的字符串中,以这个很棒的 Galantis 音乐视频 https://www.youtube.com/watch?v=5XR7naZ_zZA 为例。

然后我想在源代码中搜索特定的 div id“action-panel-details”并确认何时找到。使用下面的代码,整个页面只需加载到我在服务器上运行的页面上。

file_get_contents() 甚至可以做到这一点吗?这是加载页面、视频和所有内容的代码:

<?php

$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');

if(preg_match("~action-panel-details~", $str)){
echo "it's there";
}

?>

我也尝试过使用 simplexml_load_file() 并最终出现此错误:

Warning: simplexml_load_string(): Entity: line 1: parser error : xmlParseEntityRef: no name in /page.php on line 5

Warning: simplexml_load_string(): ndow, document);</script><script>var ytcfg = {d: function() {return (window.yt & in /page.php on line 5

Warning: simplexml_load_string(): ^ in /page.php on line 5

Warning: simplexml_load_string(): Entity: line 1: parser error : xmlParseEntityRef: no name in /page.php on line 5

这是产生它的代码:

<?php

$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');

$str = simplexml_load_string($str);

if(preg_match("~watch-time-text~", $str)){
echo "it's there";
}

?>

非常感谢任何帮助。

【问题讨论】:

    标签: php preg-match file-get-contents simplexml


    【解决方案1】:

    也许使用 curl:

    //$url = 'https://www.youtube.com/';
    $url = "https://www.youtube.com/watch?v=5XR7naZ_zZA";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $content = curl_exec($ch);
    curl_close($ch);
    
    if(preg_match("~watch-time-text~", $content)){
        echo "it's there";
    }else{
        echo 'is another page';
    }
    
    print document code:
    echo "<pre>".htmlentities($content)."<pre>";
    //
    match whit html code in 'watch-time-text':
    <div id="action-panel-details" class="action-panel-content yt-uix-expander 
    yt-uix-expander-collapsed yt-card yt-card-has-padding">
    <div id="watch-description" class="yt-uix-button-panel">
    <div id="watch-description-content">
    <div id="watch-description-clip"><span id="watch-description-badges"></span>
    <div id="watch-uploader-info"><strong class="watch-time-text">
    

    【讨论】:

    • 感谢您的回复。
    【解决方案2】:

    是的,你非常接近。基本上,只需废弃您尝试将其加载到 XML 中的部分,因为页面代码是 HTML 而不是 XML。

    $str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');
    
    if(preg_match("~watch-time-text~", $str)){
        print "Match was found!";
    }
    else {
        print "No match was found. :(";
    }
    

    这将显示:

    Match was found!
    

    很遗憾,由于ideone.comcodepad.org 不允许我使用file_get_contents,因此我无法向您展示演示,但这可以在我自己的服务器上使用。

    如果您遇到像我一样不允许file_get_contents 的情况,您可以按照 miglio 所说的方式使用 cURL 获取远程源。但其余的都是一样的:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/watch?v=5XR7naZ_zZA');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $str = curl_exec($ch);
    curl_close($ch);
    
    
    if(preg_match("~watch-time-text~", $str)){
        print "Match was found!";
    }
    else {
        print "No match was found. :(";
    }
    

    【讨论】:

    • 非常感谢。第一个解决方案对我有用。
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多