Вопрос

I'm using SOAP api for allegro.pl (polish alternative for ebay.com) and I've a litte problem with sending images through SOAP. I was asking the same question at official forum but unfortunately no-one has replied.

Anyway, working (piece of) code (soap message) in PHP looks like that:

array(
'fid' => 16,
'fvalueImage' => file_get_contents('http://www.stropna_www.pl/images/obrazek.jpg') 

note this is not a full soap message, but this one which made me not sleeping this night

And it works (well, in documentation it works, I cannot be sure as I do not use PHP (neither do I know it)). I was looking for equivalent for file_get_contents in ruby and I was trying to use net/http and open-uri to open file from the url and even tried to use

img = File.open('img.jpg', 'rb') { |f| f.read }

but every time I try, server returns invalid XML.

When I try to send the request without image it works correctly.


Here's the full code in Ruby:

def self.do_new_auction_ext 
img = File.open('img.jpg', 'rb') { |f| f.read }

xml_message = prepare_message("DoNewAuctionExtRequest", {
  :sessionHandle => self.session_id,
  :fields => [
    { :fid => 1,                                # Tytuł
      :fvalueString => "Oferta Testowa ze zdjeciem", },       # TODO: Zmienne?
    { :fid => 2,                                # Kategoria
      :fvalueInt => 1834 },                       # TODO: Zmienne?
    { :fid => 4,                                # Czas trwania
      :fvalueInt => 3 },                          # 1 - 3 dni, 2 - 7 dni, 3 - 10 dni
    { :fid => 5,                                # Ilość
      :fvalueInt => 12},                          # TODO: Zmienne?
    { :fid => 8,                                # Cena kup teraz
      :fvalueFloat => 50.00},                     # TODO: Zmienne?
    { :fid => 9,                                # Kraj
      :fvalueInt => 228},                         # 1 - Polska, 228 - Neverland
    { :fid => 10,                               # Wojewodztwo
      :fvalueInt => 215},                         # Lewopolskie
    { :fid => 11,                               # Miejscowość
      :fvalueString => "Bielsko-Biala"},
    { :fid => 14,                               # Formy platnosci
      :fvalueInt => 1 },                          # Płatne z góry przelewem
    { :fid => 16,                               # Zdjecie 1
      :fvalueString => img },
    #{ :fid => 15,                               # Opcje dodatkowe
    #  :fvalueInt => 2},
    { :fid => 24,                               # Opis
      :fvalueString => "konik"},
    { :fid => 28,                               # Sztuki / Komplety / Pary
      :fvalueInt => 1},
    { :fid => 32,                               # Kod pocztowy
      :fvalueString => "43-300"},
    { :fid => 35,                               # Darmowe opcje przesylki
      :fvalueInt => 2}                          # 1 - Odbior osobisty, 2 - e-mail, 4 - Odbior osobisty po przedplacie
  ]
})

# puts xml_message

response = @client.call(:do_new_auction_ext) do
  xml xml_message
end

don't mind polish comments

prepare_message function only creates xml which goes to the server, and I'm quite sure it's working correctly as w/o image the server responds as it should (it works!)

but, with the image sent the server responded with

{:fault=>{:faultcode=>"Sender", :faultstring=>"Invalid XML"}}

and here is website with working PHP code website is in polish, but code is in PHP

and lastly, here is the documentation it's also in polish, but includes english naming and additional PHP code under "Przykłady wywołań" tab

I'm using Ruby 1.9.3 and savon to send all SOAP request (which works, but sending the image doesn't).


Apologies for the wall of text but I was trying to explain this best I can. Thanks in advance for any help, and thank you even for reading this :)

Это было полезно?

Решение

I'd try to encode the image content like:

Base64.encode64(File.binread('path_to_your_file')

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top