微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 从外部网站获取DIV内容

我想从纯 PHP的外部网站获得DIV.

外部网站:http://www.isitdownrightnow.com/youtube.com.html

我希望来自isitdownrightNow(statusup div)的Div文本:< div class =“statusup”>该网站可能只适合您…< / div>

我已经尝试过使用DOMDocument和str_get_html的file_get_contents,但我无法让它工作.

例如这个

$page = file_get_contents('http://css-tricks.com/forums/topic/jquery-selector-div-variable/');
    $doc = new DOMDocument();
    $doc->loadHTML($page);
    $divs = $doc->getElementsByTagName('div');
    foreach($divs as $div) {
        // Loop through the DIVs looking for one withan id of "content"
        // Then echo out its contents (pardon the pun)
        if ($div->getAttribute('class') === 'bbp-template-notice') {
             echo $div->nodeValue;
        }
    }

它只会在控制台中显示错误

Failed to load resource: the server responded with a status of 500
(Internal Server Error)

解决方法

这就是我经常使用的:

$url = 'https://somedomain.com/somesite/';
$content = file_get_contents($url);
$first_step = explode( '<div id="thediv">',$content );
$second_step = explode("</div>",$first_step[1] );

echo $second_step[0];

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐