2011-07-18 172 views
1

我有一個Java應用程序(服務器控制器),我需要與PHP進行反覆通信,並且想知道如何執行此操作的最佳方法?PHP和Java之間的通信

我最初的想法是使用XML,並且PHP和Java兩端都通過HTTP向對方發送XML消息,但是這種方法似乎相當複雜。有誰知道任何其他方法或任何這樣做? Tomcat會是個好主意嗎?那麼PHP/Java Bridge

+2

XML的得到流行語達標,但還有更好/更高效的數據交換格式,特別是如果交換的雙方都在你的控制。 –

+0

嘗試[PHP/Java Bridge](http://php-java-bridge.sourceforge.net/pjb/)項目。 – Felix

回答

5

如果它是一個基於Web的Java應用程序,我會實現一個簡單的REST API,然後在PHP中使用CURL。

+1

最好的解決方案,因爲CURL易於在PHP中掌握,而不是完整的soap web服務 –

1

你可以使用soap來使用標準的XML,我想你需要一個像tomcat或者jboss這樣的應用服務器,並且在java端定義一些服務。

PHP有SoapClient類可擴展到易於實現接口

0
**Step1.** Create php file and put it inside www folder if you are using wamp server(Enable SSL on wamp if you want to communicate on SSL). 

**phpFile.php** 


<?PHP 
// connect via SSL, but don't check cert 
$url=curl_init('https://www.google.co.in/?gws_rd=ssl'); 
curl_setopt($url, CURLOPT_POST, 1); 
curl_setopt($url, CURLOPT_CUSTOMREQUEST, 'POST'); //without this line the request is being sent with GET method (I can see that with curl_getinfo) 
curl_setopt($url, CURLOPT_PORT, 443); 
curl_setopt($url, CURLOPT_VERBOSE, true); 
curl_setopt($url, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($url, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($url, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded;")); 
curl_setopt($url, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 Fiddler'); 
curl_setopt($url, CURLOPT_POST, 1); 
$content = curl_exec($url); 
if(curl_error($url)) 
{ 
echo "Error: >> ".curl_error($url); 
exit; 
} 
echo $content; // show target page 
?> 



**Step2. Create java file like** 



package com.ravisoft.test; 

import java.io.DataOutputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLContext; 
import javax.net.ssl.SSLSession; 
import javax.net.ssl.TrustManager; 
import javax.net.ssl.X509TrustManager; 

import java.security.cert.X509Certificate; 


public class PHPCurlSSL { 
    public static void main(String[] args) throws Exception { 
     DataOutputStream outStream; 
    /* 
    * fix for 
    * Exception in thread "main" javax.net.ssl.SSLHandshakeException: 
    *  sun.security.validator.ValidatorException: 
    *   PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
    *    unable to find valid certification path to requested target 
    */ 
    TrustManager[] trustAllCerts = new TrustManager[] { 
     new X509TrustManager() { 
      public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return null; 
      } 

      public void checkClientTrusted(X509Certificate[] certs, String authType) { } 

      public void checkServerTrusted(X509Certificate[] certs, String authType) { } 

     } 
    }; 

    //String body = "reqUrl=" + URLEncoder.encode("https:// your URL", "UTF-8"); 

    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

    // Create all-trusting host name verifier 
    HostnameVerifier allHostsValid = new HostnameVerifier() { 
     public boolean verify(String hostname, SSLSession session) { 
      return true; 
     } 
    }; 
    // Install the all-trusting host verifier 
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 
    /* 
    * end of the fix 
    */ 

    URL url = new URL("https://localhost/phpFile.php"); 

    URLConnection con = url.openConnection(); 
    ((HttpURLConnection)con).setRequestMethod("POST"); 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setUseCaches(false); 
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    //con.setRequestProperty("Content-Length", ""+ body.length()); 
    //outStream = new DataOutputStream(con.getOutputStream()); 
// Send request 
    //outStream.writeBytes(body); 
    //outStream.flush(); 
    //outStream.close(); 
    Reader reader = new InputStreamReader(con.getInputStream()); 
    while (true) { 
     int ch = reader.read(); 
     if (ch==-1) { 
     break; 
     } 
     System.out.print((char)ch); 
    } 
    } 
} 




**Step 3. Some other steps like.** 
a. You may need to change your port 
b. You may need to create ssl certificate 
c. change php.ini in apache and php in wamp to enable curl and ssl. 
d. You can send arguments from java to php 
+0

請僅在代碼周圍使用代碼塊,並在代碼塊外部留下解釋以更好地閱讀答案 –