I need to gather all this data from a specific site. I need the URL, Image, Text. Here is the code I am trying to use. But I need to gather all the pieces of info from all the tags on the page.
<article> <a href="http://www.link.com"> <div><img src="https://image.com/image.png" /></div> <div>History</div> <div><h3>Content Here.</h3></div> </article> <article> <a href="http://www.link.com"> <div><img src="https://image.com/image.png" /></div> <div>History</div> <div><h3>Content Here.</h3></div> </article> <article> <a href="http://www.link.com"> <div><img src="https://image.com/image.png" /></div> <div>History</div> <div><h3>Content Here.</h3></div> </article>
php code
$html = file_get_contents($feed_url); $dom = new DOMDocument(); @$dom->loadHTML($html); $xpath = new DomXPath($dom); $articles = $xpath->query("//article"); $items = array(); foreach($articles as $article) { $link = $xpath->query("//a/@href", $article); $img = $xpath->query("//img/@src", $article); $link = $xpath->query("//h3", $article); }
I can’t seem to get this to return any values. I can get a single value through the foreach. But I need all the others as well. I can’t quite figure out how to make this happen. Any help would be highly appreciated.
Advertisement
Answer
If I change your foreach
loop like this:
foreach($articles as $article) { $link = $xpath->query(".//a/@href", $article); $img = $xpath->query(".//img/@src", $article); $head = $xpath->query(".//h3", $article); echo $link[0]->nodeValue . " ". $img[0]->nodeValue . " ". $head[0]->nodeValue . "n"; }
I get this as output (I added numbers to the elements just to distinguish the tree <article>
nodes:
http://www.link1.com https://image.com/image1.png Content1 Here. http://www.link2.com https://image.com/image2.png Content2 Here. http://www.link3.com https://image.com/image3.png Content3 Here.
Is that what you’re looking for?