Schlagwort-Archiv: gem

Using Magento SOAP-API with Ruby and Savon

Requirements

Install savon and then proceed with the ruby code snippet.

gem install savon

The code

#
# Magento API can be found @ http://www.magentocommerce.com/wiki/doc/webservices-api/api
#
#
require 'rubygems'
require 'savon'

Savon.configure do |config|
  config.log = false            # disable logging
  #config.log_level = :info      # changing the log level
  #config.logger = Rails.logger  # using the Rails logger
end

client = Savon::Client.new do
  wsdl.document = "http://HERE.COMES.YOUR.SERVER/index.php/api/?wsdl"
end

response = client.request :login do
  soap.body = { :username => 'soapuser', :apiKey => 'whatthefuck' }
end
if response.success? == false
  puts "login failed"
  System.exit(0)
end

session =  response[:login_response][:login_return];

response = client.request :call do
  soap.body = {:session => session,:method => 'catalog_product.list' }
end

# fetching all products
if response.success?
  # listing found products
  response[:call_response][:call_return][:item].each do |product|
    puts "-------------------------------------------"
    product = product[:item]
    product.each do |pkey|
        puts "#{pkey[:key]} -> #{pkey[:value]}"
    end
  end
end

#logging out
response = client.request :endSession do
  soap.body = {:session => session}
end
puts response.to_hash