import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import XmlHelper;
public class QXOutStream {
public String outPutStr(String urlStr, String input) throws Exception{
StringBuffer strBuf = new StringBuffer();
String Resulst="";
try{
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setAllowUserInteraction(false);
con.setUseCaches(false);
con.setRequestProperty("Accept-Charset", "GBK");
BufferedOutputStream bufOutPut = new BufferedOutputStream(con.getOutputStream());
byte[] bdat = input.getBytes("UTF-8");//解决中文乱码问题
bufOutPut.write(bdat, 0, bdat.length);
bufOutPut.flush();
BufferedInputStream inp = new BufferedInputStream(con.getInputStream());
InputStreamReader in = new InputStreamReader(inp,Charset.forName("GBK"));
BufferedReader bufReador = new BufferedReader(in);
String tempStr = "";
while (tempStr != null) {
strBuf.append(tempStr);
tempStr = bufReador.readLine();
}
Resulst = XmlHelper.getPostNodeText(strBuf.toString(), "OPERATOR_RESULT");//.getPostFirstRowText(strBuf.toString(), "OPERATOR_RESULT");
}
catch (Exception e) {
//System.err.println("Exception:"+e.toString());
throw e;
//return "N";
}
finally{
return Resulst;
}
}
}
你可以参考这个例子调用php 的api接口,这里面的urlStr就是你调用php的api url接口
⑵ php调用java接口,java段要求json格式的请求参数
java端取值方式错了,用流取.
br = request.getReader();
StringBuffer sb = new StringBuffer("");
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
br.close();
System.out.print(sb.toString())
⑶ Java调用php接口,入参只支持form-data格式,如何封装参数
接口返回的参数格式一般是由客户端的需要来设置,至于你说的这些,一般是封装成一个对象,然后将对象转换成Json字符串返回,客户端接收到Json字符串后,再转换成对象来解析需要的信息就可以了。
⑷ 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 对接java接口出现error fetching http headers,怎么找出问题所在
先确定是java接口程序报错还是java接口程序判断php调用时带的参数验证返回的错误
⑹ java如何调用php接口
java那边用个http客户端,
请求php这边的应用就可以了,
不过两个系统还是要约定好
通信协议
(输入/输出数据格式)!
希望回答能给你带来帮助~
如果满意,请采纳,如还有疑问,可继续追问!
您也可以向我们团队发出请求,会有更专业的人来为您解答!
⑺ java做底层服务,php调用服务,用什么形式
webservice发展了好久了,有好多种客户端部署调用方式,流程大致是先创建服务,再调用。下面的代码是创建一个简单的Webservice服务.server.phpregister('webserver');$soap->service($HTTP_RAW_POST_DATA);?>上面的代码就创建了一个Webservice服务程序,接下来创建调用Webservice接口的程序:call('webserver',$param,$web_url,$web_url);echo$ret;?>基本上流程就是这样,当然,实际应用上能写出很复杂的东西,这个你可以找找相关资料学习一下,上面的php调用Webservice程序是通用的,适合于PHP调用其它ASP.NET及Java等各类语言的Webservice接口。一些技术博文里有很详细的介绍和学习。
⑻ php 中的unpack方法 在java中有对应的方法么
安装和配置PHP/Java桥
最新的PHP/Java桥zip包可在sourceforge .net/ projects/ php-java-bridge/到,安装过程依赖于选择哪个Java通过这座桥与PHP脚本交互。
◆对于J2SE,安装非常简单:
◆安装J2SE 1.6或更高版本
◆安装PHP 5.1.4或更高版本
◆解压php-java-bridge_5.2.2_j2ee.zip包
在Java类中使用PHP脚本
为了在Java应用程序中调用PHP方法,你必须对Java API非常熟悉,这个API最重要的类是:
javax.script.ScriptEngineManager:这个类扩展了java.lang.Object类,并且为ScriptEngine类提供了实例化机制。
javax.script.ScriptEngine:这是一个Java接口,包括了每个Java ScriptEngine中期望的完整功能的函数,它是通过如AbstractScriptEngine, InteractivePhpScriptEngine, InvocablePhpScriptEngine, , PhpScriptEngine,和 PhpServletScriptEngine这些类实现的。
javax.script.Invocable:这个接口提供了一个函数允许java应用程序在脚本编译器下调用中间层代码。
提醒:在/documentation/server/documentation/API目录下有更多的类及其详细信息。
⑼ PHP如何向JAVA接口webservice发送xml请求
用curl或file_get_contents去请求,把参数构造成xml即可
function arrayToXml($arr){
$xml = "<xml>";
foreach ($arr as $key=>$val){
$xml.="<".$key.">".$val."</".$key.">";
}
$xml.="</xml>";
return $xml;
}