简单对象访问协议(Simple Object Access Protocol), 是一种轻量的、简单的、基于 XML 的协议。 它被设计成在 Web 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括 HTTP、SMTP、MIME。 它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。
Suds is a lightweight SOAP python client for consuming Web Services.
sudo easy_install suds
示例调用短信发送接口,java, XFire, SOAP WSDL, webservice
import logging
logging.basicConfig(level=logging.INFO)
from suds.client import Client
from suds.sax.element import Element
url = 'http://localhost:8080/SendSmsService?wsdl'
client = Client(url)
# custum soap headers for authentication
auth = Element('AuthenticationToken')
system = Element('system').setText('SMS')
auth.append(system)
username = Element('username').setText('sms')
auth.append(username)
password = Element('password').setText('sms')
auth.append(password)
client.set_options(soapheaders=auth)
client.service.sendSms('13912345678', 'python soap client through java soap server')
PHP 的 soap 扩展不支持自定义头的认证方式,不过可以通过自定义请求XML文件的方式来实现。以下文件需要PHP的soap扩展,请先安装后再运行。
$wsdl = 'http://localhost:8080/services/SendSmsService?wsdl';
$client = new SoapClient($wsdl);
$reqXml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://sendSms.webservice.com" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<AuthenticationToken>
<system>SMS</system>
<username>sms</username>
<password>sms</password>
</AuthenticationToken>
</SOAP-ENV:Header>
<ns1:Body>
<ns0:sendSms>
<ns0:in0>%s</ns0:in0>
<ns0:in1>%s</ns0:in1>
<ns0:in2>1</ns0:in2>
</ns0:sendSms>
</ns1:Body>
</SOAP-ENV:Envelope>
XML;
$req = sprintf($reqXml, '13912345678', 'php soap client through java soap server');
$location = 'http://localhost:8080/services/SendSmsService';
$action = 'sendSms';
$result = $client->__doRequest($req, $location, $action, SOAP_1_1);