如何使用 Ruby 在 Magento Api 上创建产品图像
我尝试使用 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::错误错误)
我的问题有什么解决方案吗?
谢谢
I am try to upload a image on magento API using Ruby.
This is my code:
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')
When I run this code I got this error:
The image contents is not valid base64 data. (SOAP::FaultError)
Is there any solution for my problem?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我对图像进行编码时,它将换行符放在行尾。这个换行符会产生问题。 Magento 需要没有换行符的编码图像。
因此,删除换行符并再次运行代码就可以了。
它对我有用!
When i encode the image, it put the newline character at the end of line.This newline character create the problem. Magento needs the encoded image without the newline character.
So remove the newline character and run the code again it will work.
Its Works for me!