如何在symfony 控制器里面创建soap web service
通过一些工具将一个控制器设置成一个soap服务将会非常简单。首先,你必须安装了php soap扩展。由于php soap扩展现在不能生成wsdl,你要么自己从头开始创建要模使用第三方生成器。
php中有一些可以使用的soap服务接口。Zend SOAP 和 NuSOAP就是其中两个。尽管php soap扩展在这些例子中被使用,通用的思想还是可以被用在其他应用接口。
SOAP 的工作是通过暴露PHP对象给外部实体的(使用soap服务的人)。首先,创建一个Helloservice类-表示你将在你的soap服务中暴露的功能。在下面这个例子中,soap服务允许客户端调用一个hello方法来发送邮件:
// src/Acme/SoapBundle/Services/HelloService.php
namespace Acme\SoapBundle\Services; class HelloService
{
private $mailer; public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
} public function hello($name)
{ $message = \Swift_Message::newInstance()
->setTo('me@example.com')
->setSubject('Hello Service')
->setBody($name . ' says hi!'); $this->mailer->send($message); return 'Hello, '.$name;
}
}
接下来,你可以配置symfony来创建该类的实例。由于该类是用来发送邮件,他需要接收一个swift_mailer实例参数。使用服务容器,你可以很好的配置一个symfony helloservice对象:
# app/config/services.yml
services:
hello_service:
class: Acme\SoapBundle\Services\HelloService
arguments: ['@mailer']
下面是一个调用soap请求的例子。假如indexAction()能通过路由成功访问到,那么wsdl文档就能被返回。
namespace Acme\SoapBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response; class HelloServiceController extends Controller
{
public function indexAction()
{
$server = new \SoapServer('/path/to/hello.wsdl');
$server->setObject($this->get('hello_service')); $response = new Response();
$response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1'); ob_start();
$server->handle();
$response->setContent(ob_get_clean()); return $response;
}
}
记下ob_start() 和 ob_get_clean()这两个方法。这些方法可以控制$server->handle()输出。这是很有必要的,因为symfony希望你的控制器返回的对象是它期望的内容。Content-Type也必须设置为"text/xml",因为这是请求端希望的返回类型。因此,你使用ob_start()缓存输出内容,使用ob_get_clean()打印出要返回的内容并清空缓存,最后返回response。
下面的例子是调用nusoap客户端的服务,假设indexAction是可以通过路由成功访问到的。
$client = new \Soapclient('http://example.com/app.php/soap?wsdl');
$result = $client->call('hello', array('name' => 'Scott'));
wsdl例子如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="urn:arnleadservicewsdl"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="urn:helloservicewsdl"> <types>
<xsd:schema targetNamespace="urn:hellowsdl">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types> <message name="helloRequest">
<part name="name" type="xsd:string" />
</message> <message name="helloResponse">
<part name="return" type="xsd:string" />
</message> <portType name="hellowsdlPortType">
<operation name="hello">
<documentation>Hello World</documentation>
<input message="tns:helloRequest"/>
<output message="tns:helloResponse"/>
</operation>
</portType> <binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="hello">
<soap:operation soapAction="urn:arnleadservicewsdl#hello" style="rpc"/> <input>
<soap:body use="encoded" namespace="urn:hellowsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input> <output>
<soap:body use="encoded" namespace="urn:hellowsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding> <service name="hellowsdl">
<port name="hellowsdlPort" binding="tns:hellowsdlBinding">
<soap:address location="http://example.com/app.php/soap" />
</port>
</service>
</definitions>
如何在symfony 控制器里面创建soap web service的更多相关文章
- 翻译-使用Spring调用SOAP Web Service
原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP ...
- 翻译-使用Spring WebService生成SOAP Web Service
原文链接:http://spring.io/guides/gs/producing-web-service/ 生成SOAP web service 该指南将带领你使用Spring创建一个基于SOAP的 ...
- 使用Java创建RESTful Web Service
REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...
- 使用Java创建RESTful Web Service(转)
REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...
- 怎样创建.NET Web Service http://blog.csdn.net/xiaoxiaohai123/article/details/1546941
为什么需要Web Service 在通过internet网购买商品后,你可能对配送方式感到迷惑不解.经常的情况是因配送问题找配送公司而消耗你的大量时间,对于配送公司而言这也不是一项增值服务. 为了解决 ...
- PHP soap Web Service 使用SoapDiscovery.class.php 生成wsdl文件
PHP soap web service 使用wsdl文件 demo: ============================================================== 服 ...
- SOAP web service用AFNetWorking实现请求
问: This is my current call to (asmx) SOAP web service: NSString *soapMessage = [NSString stringWithF ...
- spring3创建RESTFul Web Service
spring 3支持创建RESTFul Web Service,使用起来非常简单.不外乎一个@ResponseBody的问题. 例如:后台controller: 做一个JSP页面,使用ajax获取数据 ...
- REST和SOAP Web Service的区别比较
本文转载自他人的博客,ArcGIS Server 推出了 对 SOAP 和 REST两种接口(用接口类型也许并不准确)类型的支持,本文非常清晰的比较了SOAP和Rest的区别联系! ///////// ...
随机推荐
- 路由器WDS桥接教程
因为有吧友买了此款路由又不会桥接,因此做这个教程.老鸟自动路过,废话不多说,下面开始讲解. 1.wifi密码破解和路由器用户名和密码部分请自行解决,我只讲桥接部分.首先,在浏览器里输入192.168. ...
- MVC4重复提交
http://blog.sina.com.cn/s/blog_712ff52a0101f38r.html http://blog.csdn.net/litao2/article/details/868 ...
- WPF 自己动手来做安装卸载程序
原文:WPF 自己动手来做安装卸载程序 前言 说起安装程序,这也许是大家比较遗忘的部分,那么做C/S是小伙伴们,难道你们的程序真的不需要一个炫酷的安装程序么? 声明在先 本文旨在教大家以自己的方式实现 ...
- Codeforces Round #236 (Div. 2)E. Strictly Positive Matrix(402E)
E. Strictly Positive Matrix You have matrix a of size n × n. Let's number the rows of the matrix f ...
- 解决Qt5.7.0 cannot find -lGL
很久没用Qt了,这次要做一个协议编辑器,在ubuntu 14.04上安装了最新版本的Qt 5.7.0.界面改用扁平化风格,第一感觉还不错.按默认步骤创建了一个gui程序,编译运行,报了一个错:cann ...
- Oracle_表数据拆分与合并
参考文档: [1]http://blog.itpub.net/8858072/viewspace-426960/ [2]http://blog.csdn.net/mattlinsheep/articl ...
- 程序猿的道路~~(How to be a programmer?)
程序猿的道路其实很简单,主要就是三条: Learn (学习), Practice(练习), Summary(总结) 推荐给新手程序猿两篇文章: 给程序员新手的一些建议 程序员技术练级攻略 当然了,整个 ...
- 魔方公式xyz
x:(整个魔方以R的方向转动),x':(整个魔方以R'的方向转动) y:(整个魔方以U的方向转动),y':(整个魔方以U'的方向转动) z:(整个魔方以F的方向转动),z':(整个魔方以F'的方向 ...
- Dubbo亮点总结
Dubbo是阿里巴巴的一个开源RPC项目,可在http://dubbo.io进行訪问 类似的产品有Hessian.spring httpinvoke 等. Dubbo的亮点总结例如以下: 1.服务注冊 ...
- 使用spring @Scheduled注解运行定时任务、
曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...