duandie0884 2016-06-16 15:34
浏览 50

PHP file_get_contents重复使用以从XML获取图像

Website I'm developing is showing products on sale and a lot of them are being updated and imported from XML (imported/updated 2x a day).

The problem is with getting product images. Some of them do not display even if url is provided.

Some XML contain from tens of items to hundreds of them.

The code loops through each item, gets all the data and then the image.

Example code regarding getting the image :

if (@file_get_contents($i->image)) {

    $name = uniqid(rand(), true) . '.jpg';

    $img = $name;

    $url = $i->image;

    $destinationPath = public_path() . '/img/upload/Items/'.$name;

    @file_put_contents($destinationPath, @file_get_contents($url));

} else {
    // do something else if there is no image
}
  1. Is my approach wrong here ?
  2. Remote server can't keep up with file requests ? And before each request need some pause ?
  3. If items are updated 2x daily I shouldn't bother with saving images locally and give img src the external url from XML ?
  4. Better use cURL() ?
  5. If approach is accepted what improvement in needs ?

Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python