如何将 PHP SoapClient 请求示例转换为 RoR?

How to translate PHP SoapClient request example to RoR?

我想通过 API 使用一些网络服务。在文档中,我找到了一个用 PHP SoapClient 编写的示例请求。但我正在使用 RoR,而且我没有 PHP 经验。谁能告诉我应该如何在 RoR 中编写相同的内容,或者至少将其翻译成普通的 HTTP 术语?

<?php
  $soap = new SoapClient(“https://secure.przelewy24.pl/external/wsdl/service.php?wsdl”);
  $test = $soap->TestAccess(“9999”, “anuniquekeyretrievedfromprzelewy24”);
  if ($test)
    echo ‘Access granted’;
  else
    echo ‘Access denied’;
?> 

编辑:我特别想知道我应该用 TestAccess 方法做什么,因为纯 HTTP 中没有方法。我应该用 URL 加入这个名字吗?

我已经包含了我们在生产中使用的整个控制器方法作为示例,但本质上您希望将 xml/wsdl 请求作为 HTTP 请求的主体传递,然后将响应解析为 xml,或者我们在下面使用的是 rexml 以便于遍历返回的文档。

def get_chrome_styles
  require 'net/http'
  require 'net/https'
  require 'rexml/document'
  require 'rexml/formatters/pretty'

  xml = '<?xml version="1.0" encoding="UTF-8"?>
      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7b.services.chrome.com">
        <soapenv:Header/>
        <soapenv:Body>
           <urn:StylesRequest modelId="' + params[:model_id] + '">
              <urn:accountInfo number="[redacted]" secret="[redacted]" country="US" language="en" behalfOf="trace"/>
              <!--Optional:-->
           </urn:StylesRequest>
        </soapenv:Body>
      </soapenv:Envelope>'


  base_url = 'http://services.chromedata.com/Description/7b?wsdl'
  uri = URI.parse( base_url )
  http = Net::HTTP.new(uri.host, uri.port)

  request = Net::HTTP::Post.new("/Description/7b?wsdl")
  request.add_field('Content-Type', 'text/xml; charset=utf-8')
  request.body = xml
  response = http.request( request )

  doc  = REXML::Document.new(response.body)

  options = []
  doc.get_elements('//style').each do |division|
    puts division
    options << { :id => division.attributes['id'], :val => division.text }
  end

  respond_to do |format|
    format.json { render :json => options.to_json }
  end
end

为了让您的生活更轻松,请查看可以简化 SOAP 访问的 gem,例如 savon

那么代码可以翻译成

# create a client for the service
client = Savon.client(wsdl: 'https://secure.przelewy24.pl/external/wsdl/service.php?wsdl')

这将自动解析 SOAP API(在 WSDL 中定义)中提供的 client 的可能方法。要列出可能的操作,请键入

client.operations

在你的情况下,这将列出

[:test_access, :trn_refund, :trn_by_session_id, :trn_full_by_session_id, :trn_list_by_date, :trn_list_by_batch, :trn_full_by_batch, :payment_methods, :currency_exchange, :refund_by_id, :trn_register, :trn_internal_register, :check_nip, :company_register, :company_update, :batch_list, :trn_dispatch, :charge_back, :trn_check_funds, :check_merchant_funds, :transfer_merchant_funds, :verify_transaction, :register_transaction, :deny_transaction, :batch_details]

然后调用该方法,执行以下操作

response = client.call(:test_access, message: { test_access_in: 9999 })
response = client.call(:test_access, message: { 
   test_access_in: 9999 }
   test_access_out: "anuniquekeyretrievedfromprzelewy24" 
)
response.body
 => {:test_access_response=>{:return=>false}}

这得到了一个结果,但我不知道它是什么意思。