如何使用Ruby在Magento Api上创建产品图像

时间:2011-04-22 06:09:24

标签: ruby soap magento

我尝试使用Ruby上传magento API上的图像。

这是我的代码:

require 'rubygems'
require 'soap/wsdlDriver'
require 'base64'

WSDL_URL = 'http://teeshop.chandru/api/v2_soap/?wsdl=1'

soap = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver

session = soap.login('*********','*********')


a = File.read('image/CP0760-01.jpg')
enc = Base64.encode64(a)

create_image = { 'file' => {"name" => "CP0760-01.jpg", "content" => enc,"mime" => 'image/jpeg'}, "label" => "kids cloths","position" => 0, "types" => ["image,""small_image", "thumbnail" ], "exclude" => 0 }

product_image = soap.call('catalogProductAttributeMediaCreate',session,'CP0760 (P.34)',creeate_image,1,'sku')

当我运行此代码时出现此错误:

  

图像内容不是有效的base64数据。 (SOAP :: FaultError)

我的问题有解决办法吗?

由于

1 个答案:

答案 0 :(得分:0)

当我对图像进行编码时,它会将换行符放在行尾。此换行符会产生问题。 Magento需要没有换行符的编码图像。 因此,删除换行符并再次运行代码它将起作用。

它为我的作品!