php – 从外部网站获取DIV内容
发布时间:2020-12-13 21:27:40 所属栏目:PHP教程 来源:http://www.isitdownrightnow.co
导读:我想从纯 PHP的外部网站获得DIV. 外部网站:http://www.isitdownrightnow.com/youtube.com.html 我希望来自isitdownrightnow(statusup div)的Div文本: div class =“statusup”该网站可能只适合您… / div 我已经尝试过使用DOMDocument和str_get_html的file
我想从纯
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; } } 它只会在控制台中显示错误:
解决方法
这就是我经常使用的:
$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]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |