简体   繁体   English

使用PHP从XML下载所有图像?

[英]Download all Images from XML using PHP?

I load an XML file from a service provider, and then my HTML displays the images in the necessary place. 我从服务提供商处加载XML文件,然后我的HTML在必要的位置显示图像。 However, I wish to cache all of these files locally, instead of having the browser load them from the remote server each time. 但是,我希望在本地缓存所有这些文件,而不是让浏览器每次都从远程服务器加载它们。

Here is a sample of my XML file... 这是我的XML文件的示例...

feed.xml feed.xml

<URI>http://imt.boatwizard.com/images/1/14/77/3801477_-1_20120229071449_0_0.jpg</URI>
<URI>http://imt.boatwizard.com/images/1/40/6/3794006_-1_20120814035230_16_0.jpg</URI>
<URI>http://imt.boatwizard.com/images/1/21/74/4012174_-1_20120706051335_21_0.jpg</URI> 

Can someone please help me write the PHP to loop through the XML, and download each image. 有人可以帮我编写PHP以遍历XML并下载每个图像。

1) Download image 1)下载图片

2) Rename image URL in XML, to match local file. 2)用XML重命名图像URL,以匹配本地文件。

3) Save XML 3)保存XML

Thanks! 谢谢!

I guess you should do something like this 我想你应该做这样的事情

// xmlize your... ehm... xml
$xml = new SimpleXMLElement($xml_content);

// extract uri elements
$result = $xml->xpath('/URI');

// loop through uris
while(list( , $node) = each($result)) {

    // with curl functions, download every image
    curl_stuff_i_dont_remember($node);

    // move it to your folder
    rename($downloaded_img, $newpath_img);

    // if everything went ok, add a new line into the output xml
    $outxml = $outxml . '<URI>' . basename($newpath_img) . '</URI>';
}

// dump the outxml
$fp = fopen('newxml.xml', 'w+');

fwrite($fp, $outxml);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM