从外部网站获取DIV内容 [英] Get DIV content from external Website
问题描述
我想用纯PHP从外部网站获得DIV。
外部网站: http://www.isitdownrightnow.com/youtube.com.html
我想要的div文本isitdownrightnow(statusup div ):< div class =statusup>该网站可能只是为您而停放......< / div>
我已经使用 DOMDocument
和 str_get_html $ c尝试了
file_get_contents
$ c>,但我无法让它工作。
例如 $ b
$ 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){
//循环查找一个带有contentID的$ D $
//然后回显其内容(赦免双关语)
if($ div-> getAttribute('class')==='bbp-template-notice'){
echo $ div-> nodeValue;
$ p $它只会在控制台显示一个错误: / p>
无法加载资源:服务器响应的状态为500
(内部服务器错误)
解决方案这是我总是使用的:
$ 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];
I want to get a DIV from an external website with pure PHP.
External website: http://www.isitdownrightnow.com/youtube.com.html
Div text I want from isitdownrightnow (statusup div): <div class="statusup">The website is probably down just for you...</div>
I already tried file_get_contents
with DOMDocument
and str_get_html
, but I could not get it to work.
For example this
$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;
}
}
It will just display an error in the console:
Failed to load resource: the server responded with a status of 500
(Internal Server Error)
解决方案 This is what I always use:
$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];
这篇关于从外部网站获取DIV内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!