修复php图像刮刀代码,以便在不同情况下更灵活
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
我能够构建一些代码,从下面的网站抓取图像,每次图像链接都是随机的 ,并在另一个网站上镜像它。 虽然这很好用,但我无法将此格式复制到任何其他网站上。 我看到图像是用getElementbyId“文件”抓取的,但是从原始源代码中有许多引用“文件”,所以我有点卡住了。 非常了解php。 p>
我正在尝试做的是复制以下结果,但是在具有特定图像的任何网站上。 p> \ n
&lt;?php
$ html =
file_get_contents(“http://commons.wikimedia.org/wiki/Special:Random/File”);
$ dom = new DOMDocument();
$ dom-&gt; loadHTML($ html);
$ remoteImage = $ dom-&gt; getElementById(“file”) - &gt; firstChild-&gt; attributes [0] -
&gt; textContent;
header(“ Content-type:image / png“);
header('Content-Length:'。filesize($ remoteImage));
echo file_get_contents($ remoteImage);
?&gt;
code> pre>
试图弄清楚如何在本网站上重现这一点,以获得前 https:// pokemondb .net / pokedex / wartortle p>
我试图拉动wartortle.jpg p>
我最初的想法,如果不完全知道 因为我希望这在随机条件下起作用,所以图像假设被命名的是用它的标签来识别图像&lt; div class =“colset”&gt; p>
唉,插入“colset”而不是“file”并不能解决这个问题。 p>
有什么想法吗?? 非常感谢。-Wilson p>
div>
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.