简体   繁体   中英

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. 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...

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.

1) Download image

2) Rename image URL in XML, to match local file.

3) Save 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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