【问题标题】:Download all Images from XML using PHP?使用 PHP 从 XML 下载所有图像?
【发布时间】:2012-12-04 11:50:45
【问题描述】:

我从服务提供商加载一个 XML 文件,然后我的 HTML 将图像显示在必要的位置。但是,我希望在本地缓存所有这些文件,而不是让浏览器每次都从远程服务器加载它们。

这是我的 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> 

谁能帮我编写 PHP 来循环遍历 XML,并下载每个图像。

1) 下载图片

2) 在 XML 中重命名图像 URL,以匹配本地文件。

3) 保存 XML

谢谢!

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: php xml image


【解决方案1】:

我猜你应该这样做

// 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 2013-12-05
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    相关资源
    最近更新 更多