gpt4 book ai didi

Python 和 PHP SOAP 服务器

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:52 25 4
gpt4 key购买 nike

我尝试使用基于“PHP SOAP 服务器”的 SOAP 服务。我对参数传递有疑问。当它是一个标量参数时,一切都很好,但是当我尝试传递结构时却失败了。 Python 库以不同的格式创建数组。在此示例中,我使用的是 SUDS,但其他库也没有生成“正确的格式”。

服务 WSDL:http://www.drebdengi.ru/soap/dd.wsdl

PHP查询:

$client = new SoapClient('http://www.drebedengi.ru/soap/dd.wsdl', array("trace" => 1));    
$client->getRecordList(
'demo_api',
'demo@example.com',
'demo',
array(
'is_report' => false,
'is_show_duty' => true,
'r_period' => 8,
'r_how' => 1,
'r_what' => 6,
'r_currency' => 0,
'r_is_place' => 0,
'r_is_tag' => 0,
)
)
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ddengi"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getRecordList>
<apiId xsi:type="xsd:string">demo_api</apiId>
<login xsi:type="xsd:string">demo@example.com</login>
<pass xsi:type="xsd:string">demo</pass>
<params xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">is_report</key>
<value xsi:type="xsd:boolean">false</value>
</item>
<item>
<key xsi:type="xsd:string">is_show_duty</key>
<value xsi:type="xsd:boolean">true</value>
</item>
<item>
<key xsi:type="xsd:string">r_period</key>
<value xsi:type="xsd:int">8</value>
</item>
<item>
<key xsi:type="xsd:string">r_how</key>
<value xsi:type="xsd:int">1</value>
</item>
<item>
<key xsi:type="xsd:string">r_what</key>
<value xsi:type="xsd:int">6</value>
</item>
<item>
<key xsi:type="xsd:string">r_currency</key>
<value xsi:type="xsd:int">0</value>
</item>
<item>
<key xsi:type="xsd:string">r_is_place</key>
<value xsi:type="xsd:int">0</value>
</item>
<item>
<key xsi:type="xsd:string">r_is_tag</key>
<value xsi:type="xsd:int">0</value>
</item>
</params>
<idList xsi:nil="true" />
</ns1:getRecordList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用 SUDS 的 Python 查询:

client = suds.client.Client("http://www.drebedengi.ru/soap/dd.wsdl")
params = {
"is_report": False,
"is_show_duty": True,
"r_period": 8,
"r_how": 1,
"r_what": 6,
"r_currency": 0,
"r_is_place": 0,
"r_is_tag": 0
}
print client.service.getRecordList("demo_api", "demo@example.com", "demo", params)
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="urn:ddengi" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header/>
<ns1:Body>
<ns4:getRecordList>
<apiId xsi:type="ns2:string">demo_api</apiId>
<login xsi:type="ns2:string">demo@example.com</login>
<pass xsi:type="ns2:string">demo</pass>
<params xsi:type="ns0:params">
<is_report xsi:type="ns2:boolean">False</is_report>
<r_how xsi:type="ns2:int">1</r_how>
<r_currency xsi:type="ns2:int">0</r_currency>
<is_show_duty xsi:type="ns2:boolean">True</is_show_duty>
<r_is_tag xsi:type="ns2:int">0</r_is_tag>
<r_is_place xsi:type="ns2:int">0</r_is_place>
<r_what xsi:type="ns2:int">6</r_what>
<r_period xsi:type="ns2:int">8</r_period>
</params>
</ns4:getRecordList>
</ns1:Body>
</SOAP-ENV:Envelope>

我尝试使用 client.factory.create() 但它不起作用:类型列表为空。 print client 的输出是:

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913Service ( ddengiService ) tns="urn:ddengi"   Prefixes (0)   Ports (1):      (SoapPort)         Methods (28):            deleteAll(xs:string apiId, xs:string login, xs:string pass, )            deleteObject(xs:string apiId, xs:string login, xs:string pass, xs:integer id, xs:string type, )            getAccessStatus(xs:string apiId, xs:string login, xs:string pass, )            getAccumList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getBalance(xs:string apiId, xs:string login, xs:string pass, xs:anyType params, )            getCategoryList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getChangeList(xs:string apiId, xs:string login, xs:string pass, xs:string revision, )            getCurrencyList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getCurrentRevision(xs:string apiId, xs:string login, xs:string pass, )            getExpireDate(xs:string apiId, xs:string login, xs:string pass, )            getOrderList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getPlaceList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getRecordList(xs:string apiId, xs:string login, xs:string pass, xs:anyType params, xs:anyType idList, )            getRightAccess(xs:string apiId, xs:string login, xs:string pass, )            getServerSubs(xs:string url, )            getSourceList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getSubscriptionStatus(xs:string apiId, xs:string login, xs:string pass, )            getTagList(xs:string apiId, xs:string login, xs:string pass, xs:anyType idList, )            getUserIdByLogin(xs:string apiId, xs:string login, xs:string pass, )            setAccumList(xs:string apiId, xs:string login, xs:string pass, xs:string list, )            setCategoryList(xs:string apiId, xs:string login, xs:string pass, xs:anyType list, )            setCurrencyList(xs:string apiId, xs:string login, xs:string pass, xs:anyType list, )            setPaymentTransaction(xs:string apiId, xs:string login, xs:string pass, xs:string transactionReceipt, xs:string amount, )            setPlaceList(xs:string apiId, xs:string login, xs:string pass, xs:anyType list, )            setRecordList(xs:string apiId, xs:string login, xs:string pass, xs:anyType list, )            setSourceList(xs:string apiId, xs:string login, xs:string pass, xs:anyType list, )            setTagList(xs:string apiId, xs:string login, xs:string pass, xs:anyType list, )            userRegister(xs:string apiId, xs:string login, xs:string name, xs:string lang, )         Types (0):

最佳答案

这是正确的解决方案。主要痛点是:

  1. drebedengi.ru 的 WSDL 不导入 http://xml.apache.org/xml-soap 也不导入 http://schemas.xmlsoap.org/soap/encoding/。使用了这些模式,因此应该导入它们。这是由 ImportDoctor

  2. 修复的
  3. suds 库相当陈旧。我会用 suds-jurko 0.6因为它似乎是最新的。另一方面,同样的方法适用于 suds 0.4

  4. suds{http://xml.apache.org/xml-soap}Map 数据类型一无所知。因此没有办法实例化它。我通过为 xml-soap 添加我自己的模式定义来解决这个问题。我不知道它应该是什么样子,但我声明它的方式对我来说很好。

  5. drebedengi.ru 有时会打印 ns2:... 元素而不声明 ns2 的实际含义。当然,它们的意思是 http://xml.apache.org/xml-soap

第一件事是教 suds 使用 Map 类型:

from suds.store import defaultDocumentStore
from suds.xsd.sxbasic import Import as XsdImport

defaultDocumentStore.update({'xml.apache.org/xml-soap': \
"""<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://xml.apache.org/xml-soap" targetNamespace="http://xml.apache.org/xml-soap">
<xs:complexType name="Map">
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type='xs:anyType'/>
<xs:element name="value" type='xs:anyType'/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
"""})

XsdImport.bind(
'http://xml.apache.org/xml-soap',
'suds://xml.apache.org/xml-soap')

然后您需要将缺少的导入添加到 WSDL:

wsdl = 'http://www.drebedengi.ru/soap/dd.wsdl'
soapenc = Import('http://schemas.xmlsoap.org/soap/encoding/')
soapenc.filter.add(None) # dd.wsdl does not have targetNamespace, thus None here
xmlsoap = Import('http://xml.apache.org/xml-soap')
xmlsoap.filter.add(None) # dd.wsdl does not have targetNamespace, thus None here
# NOTE: replace NoCache with the appropriate cache
client = Client(wsdl, cache=NoCache(), doctor=ImportDoctor(soapenc, xmlsoap))

# Sometimes responses reference ns2, so we declare it explicitly
client.add_prefix('ns2', 'http://xml.apache.org/xml-soap')
client.add_prefix('SOAP-ENC', 'http://schemas.xmlsoap.org/soap/encoding/')

然后你可以通过 client.factory.create(...) 实例化它

raw_params = {
"is_report": False,
"is_show_duty": True,
"r_period": 8,
"r_how": 1,
"r_what": 6,
"r_currency": 0,
"r_is_place": 0,
"r_is_tag": 0
}

# Here we create the Map. Note how namespace is referenced
m = self.client.factory.create("{http://xml.apache.org/xml-soap}Map")
m['item'] = [{"key": key, "value": params[key]} for key in params]
return client.service.getRecordList("demo_api", "demo@example.com", "demo", m)

关于Python 和 PHP SOAP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com