2013-03-08 61 views
-1

我的供应商为我提供了所有产品及其图片的xml文件。动态下载图片

的图像在suplyer的DB主办,因此在XML文件看起来像www.webservice.supplier.com/index.php?action=getimage & ID = 10

我怎样才能下载此图片或在我的opencart系统中使用?

回答

0

你可以尝试打开与的file_get_contents,FOPEN或任何其他相关的PHP函数的文件。一旦打开到流中,您可以使用一组相关函数(fwrite等)将其保存。

上的细节,这是一些快速和肮脏的PHP代码。

//This is the source image... 
$url='http://www.google.es/logos/2013/womens_day_2013-1055007-hp.jpg'; 

$file=fopen($url, 'r'); //Check with your server's provider, just in case they won't allow you to do this. 

if(!$file) 
{ 
    die('ERROR: Could not read file'); 
} 
else 
{ 
    //This is where you'll store the file. Check the directory permissions. 
    $destination=fopen('results/file_in_disk_.jpg', 'w'); 

    if(!$destination) die('ERROR: Could not create or open destination file'); 
    else 
    { 
     while($data=fread($file, 1024)) 
     { 
      fwrite($destination, $data); 
     } 

     fclose($destination); 
     fclose($file); 
    } 
} 

图像提供商很可能会输出与该网址相关联的图像标头,所以不要担心,如果源url看起来不像图像。

请记住,你应该有你的供应商的下载和使用的图像许可。

希望有所帮助。

编辑:我忘了,你应该换一些其他的一块从XML文件读取并就执行该代码。不要忘记检查已经存在的图像!

+0

非常感谢!它的伟大工程,另外,如果我不得不做一些tansformation到文件...所以我去的网页www.webservice.supplier.com/index.php?action=getimage&id=10和需要产生新的URL在图像出现,之后将该网址复制并在代码 – user2120473 2013-03-08 13:24:14

+0

最后一个问题贴:如果我想将图像与URL中出现的编号重命名(www.webservice.supplier.com/index .php?action = getimage&id = 10)我该怎么办? – user2120473 2013-03-08 13:25:02

+0

对不起,我可以做,因为有很多图像吗?当然,我有我的供应商的许可 – user2120473 2013-03-08 13:27:50