Thursday 11 July 2019

get div conents from external url - best method - PHP? DOM? jQuery?




I am trying to get the contents of a div from the id name.



Here is the div I am trying to get:

...



However, this is on another external website so it has to be called with a www or an http:// etc...



I am sure it's possible. Just not sure if I should use PHP, DOM or jQuery etc..



I am thinking this code should be possible to do in a few lines. Just don't know what is the best method. Thanks for the tips or ideas.




UPDATE: It was suggested this was a duplicate question. It is not. I have used the code from the suggested duplicate question below and it does not work.



Here is one of the errors: Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID changeRegionForm already defined in Entity, line: 85 in /home/content/w/i/s/wisdom33/html/testing/getDivExternalWebsite.php on line 14



Here is a link to the code: http://massmediamail.com/testing/getDivExternalWebsite.php



Here is the code:







$doc = new DomDocument;

// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://www.lifesitenews.com/news/second-madagascar-archbishop-criticizes-catholic-relief-services-full-trans'));

var_dump($doc->getElementById('article-body'));

?>



Answer



The answer is in http://simplehtmldom.sourceforge.net/



And once you download the code from the above url and place it in the appropriate place and link to it correctly then this code at the bottom works just perfect for what I asked above.





// Note you must download the php files from the link above
// and link to them on this line below.
include_once('../simple_html_dom.php');

$html = file_get_html('http://www.lifesitenews.com/news/second-madagascar-archbishop-criticizes-catholic-relief-services-full-trans');
$elem = $html->find('div[id=article-body]', 0);
echo $elem;

?>


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...