导航:首页 > 文件教程 > phpwebservice教程

phpwebservice教程

发布时间:2022-09-28 13:42:16

① PHP如何调用webservice

最近工作中需要用php调用webservice接口,对php不熟,上网搜搜,发现关于用php调用webservice的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经包含了处理soap的模块,但是资料太少了,上php官网上查帮助,写的不是很容易理解,经过多次实践,终于搞清楚了,php调用webservice还是非常简单的。下面用一个例子说明:
webservice服务是查询qq用户是否在线
使用php5开发客户端:
<?php
try{
//$client=newSoapClient("HelloService.wsdl",array('encoding'=>'UTF-8'));
$client=newSoapClient("webservices/qqOnlineWebService.asmx?wsdl");
var_mp($client->__getFunctions());
print("<br/>");
var_mp($client->__getTypes());
print("<br/>");

classqqCheckOnline{
var$qqCode="10000";
};
$arrPara=array(newqqCheckOnline);

$arrResult=$client->__Call("qqCheckOnline",$arrPara);//$client->qqCheckOnline($arrPara);

echo$arrResult->qqCheckOnlineResult."<br/>";
}catch(SOAPFault$e){
print$e;
}
?>
代码确实很简单吧,创建SoapClient对象时,可以使用保存在本地WSDL文件,也可以使用远程的地址,后面的array数组里可以带上很多的参数,具体参数可以查php的SoapClient帮助,这里带的是字符集编码,如果调用方法的参数里有中文,一定要指定字符集编码,否则会出错。
调用webservice前可以先调用SoapClient的__geunctions()和__getTypes()方法看一下你要调用的webservice暴露的方法,参数和数据类型,需要注意的是传入的参数名一定要和soapclient里面定义的一致,否则参数是传不过去的。
需要使用SoapClient的__soapCall()或__call()方法,具体使用方法可以查php的帮助文档。如果参数要求是一个结构体,请用类代替,如上面的代码。
另外发现个问题,如果webservice方法返回的是xml格式的字符串,php接收到以后会自己把数据内容解析出来,而不是xml字符串.

② php 怎么调用esb的webservice

PHP中也可以调用WebService。这是通过一个第三方的类来实现的。
NuSOAP - Web Services Toolkit for PHP,这是一个开源的API,其中有

nusoap_base
soap_fault
XMLSchema
soapval
soap_transport_http
soap_server
wsdl
soap_parser
soapclient
这几个类,我们用到的是soapclient类

③ php怎样通过SoapClient调用webservice接口

1、首先要先配置;extension=php_soap.dll打开php.ini
2、打开一个webservice的例子
3、怎样看这个xml的文件呢,如下图
4、下面开始写代码,先实例化SoapClient
5、使用接口方法,get_object_vars得到的是指定的对象中定义的属性组成的关联数组
6、最后运行一下测试一下

④ php做客户端,java做服务端,用webservice怎么交互

.java编写webservice服务端,php作为客户端调用.
1.首先我们写一个简单的java类并发布webservice.
package com.php;
import java.util.Map;
/**
* @author yangjuqi
* @createdate 2009-5-18 下午04:43:09
*
*/
public class WebServiceImpl {
public String sendTransact(Map map) throws Exception {
System.out.println("::: Call testModel1 :::");

if(map!=null){
String bugmanifestid = StringUtil.getValue(map.get("bugmanifestid"));
String editedby = StringUtil.getValue(map.get("editedby"));
String dditeddate = StringUtil.getValue(map.get("dditeddate"));
String fullinfo = StringUtil.getValue(map.get("fullinfo"));
String action = StringUtil.getValue(map.get("action"));
System.out.println("bugmanifestid -$amp;>quot;$ +bugmanifestid);
System.out.println("editedby -$amp;>quot;$ +editedby);
System.out.println("dditeddate -$amp;>quot;$ +dditeddate);
System.out.println("fullinfo -$amp;>quot;$ +fullinfo);
System.out.println("action -$amp;>quot;$ +action);
}
return "success";
}
}
2.配置server-config.wsdd
<deployment xmlns=""
xmlns:java="">
<handler name="URLMapper"
type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="auth"
type="java:com.php.AuthenticationHandler" />
<handler name="URLLogging"
type="java:com.php.LogHandler">
<parameter name="filename" value="c:\\MyService.log" />
</handler>
<service name="IWebService" provider="java:RPC">
<parameter name="className"
value="com.php.WebServiceImpl" />
<parameter name="allowedMethods" value="*" />
<namespace>http://localhost:8088/testphpweb</namespace>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
<handler type="URLLogging" />
</requestFlow>
</transport>
</deployment>
3.发布到jboss后,访问http://localhost:8088/testphpweb/services/IWebService wsdl能看到xml文件就说明webservice发布好了。
4.写testphpweb.php文件
< php
/*
* @author juqi yang $amp;<amp;$gt;
* @create date 2009-05-18
*/
header("Content-Type: text/html; charset=GB2312");
echo " ::: PHP CALL JAVA-WEBSERVICE ::: <br$amp;>quot;$;
require_once("nusoap/lib/nusoap.php");
// 要访问的webservice路径
$NusoapWSDL="http://localhost:8088/testphpweb/services/IWebService wsdl";
// 生成客户端对象
$client = new soapclient($NusoapWSDL, true);
// 设置参数(注意:PHP只能以'数组集'方式传递参数,如果服务端是java,用Map接收)
$param = array( 'bugmanifestid' => 'E090500001',
'editedby' => '张三',
'dditeddate' => '2009-05-19',
'fullinfo' => '已联系刘德华,筹备今晚吃饭的事,等待回复',
'action' => '0');
echo "begin remote 。。。<br$amp;>quot;$;
// 调用远程方法
$result = $client->call('sendTransact', array($param));
echo "end remote 。。。<br$amp;>quot;$;
// 显示执行结果
if (!$err=$client->getError()){
echo '结果 : '.$result;
}else{
echo '错误 : '.$err;
}
>
5.启动apache,访问
php页面显示:
::: PHP CALL JAVA-WEBSERVICE :::
begin remote 。。。
end remote 。。。
结果 : success
jboss后台监视结果:
17:12:20,781 INFO [STDOUT] ::: Call testModel1 :::
17:12:20,781 INFO [STDOUT] bugmanifestid ->E090500001
17:12:20,781 INFO [STDOUT] editedby ->张三
17:12:20,781 INFO [STDOUT] dditeddate ->2009-05-19
17:12:20,781 INFO [STDOUT] fullinfo ->已联系刘德华,筹备今晚吃饭的事,等待回复
17:12:20,796 INFO [STDOUT] action ->0
到此,php作为客户端调用java写的webservice服务端完成.
二,php编写webservice服务端,java作为客户端调用.
1.编写php webservice
< php
/*
* @author juqi yang $amp;<amp;$gt;
* @create date 2009-05-18
*/
header("Content-Type: text/html; charset=GB2312");
require_once("nusoap/lib/nusoap.php");
function sendManifest($param)
{
//把接收到的数据显示出来
return "hello ".$param["projectid"]."<=$amp;>quot;$.$param["projectname"]."<=$amp;>quot;$.$param["moleid"];
}
$server = new nusoap_server();
//配置WSDL namespace
$server->configureWSDL('myservice', //服务名称
'', //tns指定的namespace,一般填写自己的URI
true, //endpoint url or false
'rpc', //服务样式
'', //传输协议,一直是这个。
'' //wsdl 'types'元素targetNamespace
);
// 注册web服务
$server->register('sendManifest', // 服务
array(
'projectid' => 'xsd:string',
'projectname' => 'xsd:string',
'moleid' => 'xsd:string',
'molepath' => 'xsd:string',
'bugtitle' => 'xsd:string',
'bugtype' => 'xsd:string',
'openedby' => 'xsd:string',
'openeddate' => 'xsd:string',
'assignedto' => 'xsd:string',
'assigneddate' => 'xsd:string',
'fixedtime' => 'xsd:string',
'fullinfo' => 'xsd:string',
'bugmanifestid' => 'xsd:string'), // 输入参数;数组,指定类型
array('resultCode' => 'xsd:string'), // 输出;数组,指定类型
'', // namespace of method
'', // soapaction
'rpc', // style
'encoded', // use
'serviceConsumeNotify' // documentation
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
>
2.启动apache后,访问 ,如果页面如下图所示,表示webservice发布好了。
3.编写java客户端CallPhpServer .java 并调用php webservice
package com.php;
import java.util.HashMap;
import java.util.Map;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
* @author yangjuqi
* @createdate 2009-5-18 下午05:31:06
*
*/
public class CallPhpServer {
/**
* 测试方法
* @return
* @throws Exception
*/
public static String callManifest() throws Exception {
System.out.println("0");
Service service = new Service();
Call call = (Call) service.createCall();
System.out.println("1");
call.setTargetEndpointAddress(new java.net.URL(""));
call.setOperationName("sendManifest");
System.out.println("2");
Map map=new HashMap();
map.put("projectid", "109");
map.put("projectname", new String("新MM国际物流平台".getBytes(),"iso-8859-1"));
map.put("moleid", "11");
map.put("molepath", new String("财务管理".getBytes(),"iso-8859-1"));
map.put("bugtitle", new String("关于总账报表数据的问题".getBytes(),"iso-8859-1"));
map.put("bugtype", "TrackThings");
map.put("openedby", "zhangsan");
map.put("openeddate", "2009-05-31");
map.put("assignedto", "liumang");
map.put("assigneddate", "2009-05-31");
map.put("fixedtime", "2009-06-03");
map.put("fullinfo", new String("现在总账报表页面下的合计数据不对,烦请抓紧事件核实确认更正,谢谢!".getBytes(),"iso-8859-1"));
map.put("bugmanifestid", "E090500001");
call.addParameter("param", org.apache.axis.Constants.SOAP_ARRAY,javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
System.out.println("3");
Object obj=call.invoke(new Object[]{map});
return obj.toString();
}
public static void main(String[] args) throws Exception {
System.out.println("::: call php webservice :::");
String str = callManifest();
String result=new String(str.getBytes("iso-8859-1"),"GBK");
System.out.println(result);
}
}
控制台显示结果:
::: call php webservice :::
0
log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
log4j:WARN Please initialize the log4j system properly.
1
2
3
hello 109<=>新MM国际物流平台<=>11
到此,java作为客户端调用php的webservice服务端完成.

⑤ PHP调用.NET的WebService 简单实例

创建一个C#的web
service,这个就不多说了,我用vs2008的wizard创建了一个最简单的,让它运行在:http://localhost/webservice1/service1.asmx
其中有个web
method像这样的:
复制代码
代码如下:
[WebMethod]
public
string
HelloWorld()
{

return
"Hello
World";
}
ok,一切就绪。在某php文件中如下写法:
php5本身就支持SOAP调用Web
Service:
<?php
//get
localization
strings
from
C#
webservice
$client
=
new
SoapClient('http://localhost/webservice1/Localization.asmx?wsdl');
echo
"Call
web
service
method
from
C#
WebService:\n";
$result
=
$client->GetLocalizationResource();
if(!is_soap_fault($result))
{
echo
"return:\n",
$result->GetLocalizationResourceResult;
}
else
{
echo
"soap
call
fault";
}
?>
这样就OK了,会继续介绍SOAP
以上所述就是本文的全部内容了,希望大家能够喜欢。
请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!

⑥ php怎么调用java的https方式的webservice接口

webservice 发展了好久了,有好多种客户端部署调用方式 ,流程大致是先创建服务,再调用。
下面的代码是创建一个简单的Webservice服务.
server.php
<?php

require_once "lib/nusoap.php";
function webserver() {
return "This is a Webservice Server";
}
$soap = new soap_server;
$soap->register('webserver');
$soap->service($HTTP_RAW_POST_DATA);
?>

上面的代码就创建了一个Webservice服务程序,接下来创建调用Webservice接口的程序:
<?php
require_once "soap/lib/nusoap.php";

$web_url = "127.0.0.1";
$client = new soapclient($web_url."/server.php");

$param = array();
$ret = $client->call('webserver', $param, $web_url, $web_url);

echo $ret;

?>
基本上流程就是这样,当然,实际应用上能写出很复杂的东西,这个你可以找找相关资料学习一下,上面的php调用Webservice程序是通用的,适合于PHP调用其它ASP.NET及Java等各类语言的Webservice接口。一些技术博文里有很详细的介绍和学习。

阅读全文

与phpwebservice教程相关的资料

热点内容
似乎影视怎么进入 浏览:1
有什么现在看电影的网址 浏览:915
国外电影地址发布网 浏览:242
外婆的家电影完整版 浏览:667
有一个什么网可以看所有电影 浏览:748
黛妃小说txt下载 浏览:71
aqdyu 浏览:399
同人小说网 小说 浏览:433
win10电脑软件自启动 浏览:836
js获取图片位置 浏览:354
vb股票实时数据在哪里 浏览:777
谁和她睡了韩剧女主谁扮演的 浏览:643
iphone4软件更新怎么取消 浏览:932
欧美电影出轨邻居 浏览:176
在线观看的网站都没有吗 浏览:326
江油宝龙广场电影院今日影讯 浏览:994
win10盘符全乱了 浏览:670
三极女鬼电影 浏览:82
linuxhtpasswd安装 浏览:142

友情链接