Reputation: 175
I was able to construct some code that grabs an image from the below website where the image link will be random every time, and mirrors it on another site. While it's great that this works, I'm unable to copy this format onto any other site. I see that the image is being grabbed with a getElementbyId "file," but from the original source code there are many many refrences to "file," so I'm a bit stuck. very knew to php still.
What I'm trying to be able to do, is replicate the below result, but on any site with a particular image.
<?php
$html =
file_get_contents("http://commons.wikimedia.org/wiki/Special:Random/File");
$dom = new DOMDocument();
$dom->loadHTML($html);
$remoteImage = $dom->getElementById("file")->firstChild->attributes[0]-
>textContent;
header("Content-type: image/png");
header('Content-Length: ' . filesize($remoteImage));
echo file_get_contents($remoteImage);
?>
Trying to figure out how I could reproduce that on this site for ex https://pokemondb.net/pokedex/wartortle
where I'm trying to pull the wartortle.jpg
My initial idea if not knowing exactly what the image would hypothetically be named, since I want this to work during random conditions, is to identify the image with it's tag < div class="colset">
Alas, plugging in "colset" instead of "file" didn't do the trick though.
Any thoughts?? Thanks so much.-Wilson
Upvotes: 0
Views: 36
Reputation: 57131
Using XPath is always a lot more flexible (although probably slower than other solutions). Using the previous example you could use the following to get the file name...
<?php
ob_start();
$doc = new DOMDocument;
$doc->loadHTMLFile('https://pokemondb.net/pokedex/wartortle');
$xpath = new DOMXPath($doc);
$query = "//li[@id='svtabs_basic_8']//img/@src";
ob_end_clean();
header('content-type: image/jpeg');
$entries = $xpath->query($query);
foreach ($entries as $entry) {
readfile((string)$entry->value);
}
I've added the ob_start and ob_end_clean to remove the xml validation errors.
Upvotes: 1