繁体   English   中英

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

[英]How to translate PHP SoapClient request example to RoR?

我想通过其API使用某些Web服务。 在文档中,我找到了一个用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连在一起吗?

为了使您的生活更轻松,请查看宝石,如savon ,它可以简化SOAP访问。

然后可以将代码翻译为

# 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}}

这得到了结果,但是我不知道这意味着什么。

我已经包含了一个完整的控制器方法,我们在生产中使用了该方法作为示例,但实际上您想将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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM