導航:首頁 > 編程語言 > php調用java介面

php調用java介面

發布時間:2023-01-12 15:06:01

java 怎麼調用php的api介面

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;
}

閱讀全文

與php調用java介面相關的資料

熱點內容
蘋果如何創建伺服器錯誤 瀏覽:492
軟考初級程序員大題分值 瀏覽:473
js壓縮視頻文件 瀏覽:578
linux如何通過命令創建文件 瀏覽:989
應用加密app還能訪問應用嘛 瀏覽:432
安卓怎麼用支付寶交違章罰款 瀏覽:665
php面向對象的程序設計 瀏覽:504
數據挖掘演算法書籍推薦 瀏覽:894
投訴聯通用什麼app 瀏覽:150
web伺服器變更ip地址 瀏覽:954
java正則表達式驗證郵箱 瀏覽:360
成熟商務男裝下載什麼軟體app 瀏覽:609
加密2h代表長度是多少厘米 瀏覽:23
拍賣程序員 瀏覽:101
電腦的圖片放在哪個文件夾 瀏覽:274
unsignedintjava 瀏覽:216
編譯器下載地址 瀏覽:42
什麼是面對對象編程 瀏覽:708
b站伺服器什麼時候恢復 瀏覽:721
6p相當於安卓機什麼水準 瀏覽:499