php下webservice使用总结
基于thinkphp3.2的
1.修改php配置 php.ini
extension=php_soap.dll
soap.wsdl_cache_enabled=0
2.soap有两种模式 wsdl和 no-wsdl
(1)wsdl
首先,先生成wsdl文件
生成wsdl的方法
<?php
namespace Api\Controller;
use Api\Service\SoapDiscovery;
use Think\Controller; class CreatewsController extends Controller
{
public function index()
{
$disco = new SoapDiscovery('\\Api\\Controller\\ServerController', 'soap'); //第一个参数是类名(生成的wsdl文件就是以它来命名的),即Service类,第二个参数是服务的名字(这个可以随便写)。
$r = $disco->getWSDL();
exit();
}
}
ServerController.class.php
<?php
namespace Api\Controller;
use Think\Controller;
class ServerController extends Controller
{
public function hello1()
{
return 'hello good';
}
public function sum1($a = 0, $b = 1)
{
return $a + $b;
}
}
SoapDiscovery.class.php
<?php
namespace Api\Service;
/**
* SoapDiscovery Class that provides Web Service Definition Language (WSDL).
*
* @package SoapDiscovery
* @author Braulio Jos?Solano Rojas
* @copyright Copyright (c) 2005 Braulio Jos?Solano Rojas
* @version $Id$
* @access public
**/
class SoapDiscovery {
private $class_name = '';
private $service_name = ''; /**
* SoapDiscovery::__construct() SoapDiscovery class Constructor.
*
* @param string $class_name
* @param string $service_name
**/
public function __construct($class_name = '', $service_name = '') {
$this->class_name = $class_name;
$this->service_name = $service_name;
} /**
* SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
*
* @return string
**/
public function getWSDL() {
if (empty($this->service_name)) {
throw new Exception('No service name.');
}
$headerWSDL = "<?xml version=\"1.0\" ?>\n";
$headerWSDL.= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
$headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n"; if (empty($this->class_name)) {
throw new Exception('No class name.');
} $class = new \ReflectionClass($this->class_name); if (!$class->isInstantiable()) {
throw new Exception('Class is not instantiable.');
} $methods = $class->getMethods(); $ws_url = '/api/index/ws?wsdl';
$host = 'http://'.$_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
$tmp = str_replace('\\', '', dirname($_SERVER['SCRIPT_NAME']));
$tmp = empty($tmp) ? '' : '/' . trim($tmp, '/');
$host .= $tmp;
$site = $host.'/index.php';
$ws_url = $site.$ws_url; $portTypeWSDL = '<portType name="'.$this->service_name.'Port">';
$bindingWSDL = '<binding name="'.$this->service_name.'Binding" type="tns:'.$this->service_name."Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
//$serviceWSDL = '<service name="'.$this->service_name."\">\n<documentation />\n<port name=\"".$this->service_name.'Port" binding="tns:'.$this->service_name."Binding\"><soap:address location=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."\" />\n</port>\n</service>\n"; $serviceWSDL = '<service name="'.$this->service_name."\">\n<documentation />\n<port name=\"".$this->service_name.'Port" binding="tns:'.$this->service_name."Binding\"><soap:address location=\"{$ws_url}\" />\n</port>\n</service>\n";
$messageWSDL = '';
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL.= '<operation name="'.$method->getName()."\">\n".'<input message="tns:'.$method->getName()."Request\" />\n<output message=\"tns:".$method->getName()."Response\" />\n</operation>\n";
$bindingWSDL.= '<operation name="'.$method->getName()."\">\n".'<soap:operation soapAction="urn:'.$this->service_name.'#'.$this->class_name.'#'.$method->getName()."\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";
$messageWSDL.= '<message name="'.$method->getName()."Request\">\n";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$messageWSDL.= '<part name="'.$parameter->getName()."\" type=\"xsd:string\" />\n";
}
$messageWSDL.= "</message>\n";
$messageWSDL.= '<message name="'.$method->getName()."Response\">\n";
$messageWSDL.= '<part name="'.$method->getName()."\" type=\"xsd:string\" />\n";
$messageWSDL.= "</message>\n";
}
}
$portTypeWSDL.= "</portType>\n";
$bindingWSDL.= "</binding>\n";
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
$fso = fopen(dirname(__FILE__) . "/SoapService.wsdl", "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
} /**
* SoapDiscovery::getDiscovery() Returns discovery of WSDL.
*
* @return string
**/
public function getDiscovery() {
return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."?wsdl\" />\n</disco:discovery>";
}
}
server端
<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller
{public function ws()
{
ob_clean();
$server = new \SoapServer(dirname(dirname(dirname(__FILE__))) . '/\Api\Service\SoapService.wsdl', array('soap_version' => SOAP_1_2, 'trace' => true, 'exceptions' => true));
$server->setClass('\Api\Controller\ServerController'); //注册ServerController类的所有方法
$server->handle(); //处理请求
}
}
client端
<?php
namespace Api\Controller; use Think\Controller; class IndexController extends Controller
{
public function testWsdl()
{
try {
$soap = new \SoapClient("http://127.0.0.102:80/index.php/api/index/ws?wsdl", array(
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE,
));
echo $soap->sum1(12, 2);
} catch (Exction $e) {
echo print_r($e->getMessage(), true);
}
}
}
(2)no-wsdl
server
<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller
{
public function ws()
{
ob_clean();
$arr = array('uri' => "abc");
$server = new \SoapServer(null, $arr);
$server->setClass('\Api\Controller\ServerController'); //注册Service类的所有方法
$server->handle(); //处理请求
exit();
}
}
client
<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller
{
public function testNonWsdl()
{
try {
$soap = new \SoapClient(null, array(
"location" => "http://127.0.0.102:80/index.php/api/index/ws",
"uri" => "abc", //资源描述符服务器和客户端必须对应
"style" => SOAP_RPC,
"use" => SOAP_ENCODED,
));
echo $soap->sum1(12, 2);
} catch (Exction $e) {
echo print_r($e->getMessage(), true);
}
}
}
目录结构
补充:
1. 报错looks like we got no XML document in
(1)
php.ini中的 always_populate_raw_post_data = - 注释去掉
(2)服务器端代码出错,只要有错,就会报上面的提示,仔细检查服务器端代码语法问题即可解决
(3)确保服务器端没有任何输出
2.偶尔出现 Warning: SoapClient::SoapClient(): I/O warning : failed to load external entity
在网上查找资料看到
PHP程序作为 SOAP客户端 采用 WSDL 模式访问远端服务器的时候,PHP是通过调用 libcurl 实现的。至少在 PHP5.2.X 是这样的
如果采用 non-WSDL 模式,就不需要 libcurl。除了 了ibcurl以外,至少还关联的库包括:libidn,ibgcc,libiconv,libintl,openssl
但是我改成non-WSDL也没解决问题
最后发现是,增加xml转化的函数里,增加了libxml_disable_entity_loader(true);
所以才会出现,第一次调用成功,发送普通的字符串也没问题,只有发送xml数据才会出现错误
3.输出需要请求的方法和携带的参数(适用于wsdl的形式)
try {
$client = new \SoapClient(''http://127.0.0.102:80/index.php/api/index/ws?wsdl, array(
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE,
));
echo 'SOAP types';
var_dump($client->__getTypes());
echo 'SOAP Functions';
var_dump($client->__getFunctions());
} catch (Exction $e) {
echo print_r($e->getMessage(), true);
}
4. 调用.net service方法必须传入命名参数;而调用php web服务方法,一定不能传入命名参数,只能按顺序传入
调用net的webservice
$params = [
'strXml'=>$xml_data,
'strType'=>$sType
];
$result = $client->HandleBIMInfo($params);
调用php的webservice
$result = $client->HandleBIMInfo($xml_data, $sType);
php下webservice使用总结的更多相关文章
- 今天研究了下webservice 终于OK了
今天研究了下webservice 终于OK了,所以把它写到自己的博客来,因为网上说的都很复杂 而在这里,我会很简单的说明,一看就懂 首先在进行webservice 一定要下载包 ...
- 传统模式下WebService与WebAPI的相同与不同
1.WebService是利用HTTP管道实现了RPC的一种规范形式,放弃了对HTTP原生特征与语义的完备支持:而WebAPI是要保留HTTP原生特征与语义的同时实现RPC,但WebAPI的实现风格可 ...
- C语言下WebService的使用方式
用gSoap工具: 1.在dos环境中到gSoap工具对应的目录gsoap_2.8.18\gsoap-2.8\gsoap\bin\win32路径下,执行wsdl2h -c -o *.h ht ...
- .net 下webservice 的WebMethod的属性
WebMethod有6个属性: .Description .EnableSession .MessageName .TransactionOption .CacheDuration .BufferRe ...
- PHP webservice的使用
提到php的webservice.之前还是比较陌生的,因为接触的少呀,几乎在所有的公司中没用过,仅仅用过的一次好像是接入一个第三方的短信通道,用的是SOAP|WSDL. 一个很极端的话“webserv ...
- WebService开发
一.什么是WebService: 简单通俗来说,就是企业之间.网站之间通过Internet来访问并使用在线服务,一些数据,由于安全性问题,不能提供数据库给其他单位使用,这时候可以使 用WebSer ...
- myeclipse构建webservice项目
新建server端 1 创建Web Service Project项目 2.项目名称:HelloWorldServer 3.创建接口类 4.发布 选择项目名称,选择从Java类中构建web servi ...
- webservice报错Message part refundRequest was not recognized. (Does it exist in service WSDL?)
最近在做一个支付的接口. 因为接口方使用webservice交互. 我只能去学习了下webservice 现在出了一个很古怪的问题~ 我在请求他们url的时候, 返回给我找不到控制名错误 Mess ...
- rest版的webservice
为了学习app做打算 今天就自学了下webservice,rest应该是其中一种 还有种就是soap,目前就先举个rest的demo吧 准备ws的jar和spring的jar,如何要连接数据的话就自行 ...
随机推荐
- [转]Extjs中的迭代方法
原文地址:http://www.veryhuo.com/a/view/36701.html EXTJS 有很多的迭代方法,例如,你也许已知道的Ext.each,但还有另外一些不为人知且很有用的方法.首 ...
- [转]oracle制定定时任务(dbms_jobs)
原文地址:http://www.cnblogs.com/siashan/p/4183868.html 本节摘要:本节介绍使用oracle自带的job来实现oracle定制定时执行任务. 1.引言 ...
- C语言 · 3000米排名预测
算法提高 3000米排名预测 时间限制:1.0s 内存限制:256.0MB 问题描述 3000米长跑时,围观党们兴高采烈地预测着最后的排名.因为他们来自不同的班,对所有运动员不一定都 ...
- Aspose.Cells Smart markers 基于模板导出Excel
Aspose.Cells可以预先定义Excel模板,然后填充数据(官方文档:http://www.aspose.com/docs/display/cellsjava/Smart+Markers). 设 ...
- ASP.NET MVC分页 Ajax+JsRender
前段时间整mvc的分页,倒是很顺利,参考了以下几篇博客,启发很大. http://www.cnblogs.com/tangmingjun/archive/2012/05/30/2526301.html ...
- Mysql5.7主主互备安装配置
一.安装说明 ======================================================================================= 环境: ...
- jquery 笔记总结
一.jquery 事件 <script type="text/javascript" src="/jquery/jquery.js"></sc ...
- 基于云端的通用权限管理系统,SAAS服务,基于SAAS的权限管理,基于SAAS的单点登录SSO,企业单点登录,企业系统监控,企业授权认证中心
基于云端的通用权限管理系统 SAAS服务 基于SAAS的权限管理 基于SAAS的单点登录SSO 基于.Net的SSO,单点登录系统,提供SAAS服务 基于Extjs 4.2 的企业信息管理系统 基于E ...
- freemarker自己定义标签报错(二)
freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Unexpected end of file reached. at freemar ...
- java生成word的完美解决方案
http://www.360doc.com/content/13/0731/10/13247663_303740756.shtml —————————————————————————————————— ...