简体   繁体   中英

How do you get a return value from a java file called in php

I'm using HttpURLConnection in PHP to call a Java file . In JAVA how do I return the field contents (String) and in PHP how do I receive them? Thank you. Awesome points to any who can help. Lol.

Sample code:

Java:

   @Controller
@RequestMapping("/rest")
public class ProjectController {


    @RequestMapping(value = "/ediresponceback", method = RequestMethod.POST)
    public String ediResponceBack(HttpServletRequest webRequest) throws FileNotFoundException, IOException {
        ParseEDI271 myParser = new ParseEDI271();
        if (webRequest.getParameter("edistring") != null) {
            myParser.setEDI271(webRequest.getParameter("edistring"));
        } else {
            myParser.setEDI271("Responce not found");
        }
        try {
            myParser.parseEDI();
        } catch (FormatException e) {
            e.printStackTrace();
        }

        String ediTextMessage = myParser.getEdiTextMessage();

        return ediTextMessage;
    }

}file?
        con.disconnect();
    }

PHP:

  <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>

<!--     
    http://phpfiddle.org
-->

<script type="text/javascript">

</script>

<style type="text/css">

</style>
</head>

<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" action="http://localhost:8080/EDI271Parser/rest/ediresponceback/" method="post">


    <input type="hidden" name="edistring" value="ISA|00|          |00|          |ZZ|ZIRMED         |ZZ|113394         |170307|0309|}|00501|000376343|0|P|^~GS|HB|ZIRMED|113394|20170307|0309|376335|X|005010X279A1~ST|271|0001|005010X279A1~BHT|0022|11|372490|20170307|030929~HL|1||20|1~NM1|PR|2|ALLSAVERS INSURANCE COMPANY|||||PI|AMMEDS~PER|IC|CUSTOMER SERVICE|TE|8002325432~HL|2|1|21|1~NM1|1P|2|LIFEBRITE LABORATORIES, LLC|||||XX|1609273317~HL|3|2|22|0~TRN|2|070313391|3030240928~TRN|2|549271659|9ZIRMEDCOM|ELR ID~TRN|1|540911230|9ZIRMEDCOM|ELI ID~NM1|IL|1|ALEXANDER|ARTHUR|L|||MI|005267029~N3|13180 COOPER CT~N4|HAMPTON|GA|30228~DMG|D8|19700506|M~DTP|346|D8|20160101~EB|1|IND|30||EPO E300030~MSG|INFORMATION PROVIDED IN A 271 IS NOT A GUARANTEE OF PAYMENT OR COVERAGE IN ANY SPECIFIC AMOUNT. ACTUAL BENEFITS DEPEND ON VARIOUS FACTORS, INCLUDING COMPLIANCE WITH APPLICABLE STATE REQUIREMENTS; DATE(S) OF SERVICES RENDERED AND BENEFIT PLAN TERMS AND CONDITIONS.~EB|1|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030||||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|1|IND|47}48}50}86||EPO E300030||||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|I|IND|35}AL||EPO E300030||||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|I|IND|1}33}35}98}AL}MH}UC||EPO E300030||||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|U|IND|88||0 DED-15/35/75/250 5500||||||U~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~LS|2120~NM1|VN|2|OptumRx~LE|2120~EB|A|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030|||0|||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|A|IND|47}48}50}86||EPO E300030|||0|||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|1}98}MH||EPO E300030||30||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|33}47}48}50||EPO E300030||0||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|86||EPO E300030||300||||U~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|UC||EPO E300030||100||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|47}48}50||EPO E300030||0||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030|27|0||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|47}48}50}86||EPO E300030|27|0||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030|29|0||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|47}48}50}86||EPO E300030|29|0||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|F|IND|33||EPO E300030|23|||VS|30|U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|30||EPO E300030|23|3000||||N|Y~EB|C|IND|30||EPO E300030|23|0||||N|N~EB|C|IND|30||EPO E300030|29|3000||||N|Y~EB|C|IND|30"
                + "||EPO E300030|29|0||||N|N~EB|G|IND|30||EPO E300030|29|5500||||N|Y~EB|G|IND|30||EPO E300030|29|0||||N|N~SE|62|0001~GE|1|376335~IEA|1|000376343~" /> 


    <button id="mysubmit" type="submit">Submit</button><br /><br />

</form>
</div>

<?php

?>

</body>
</html>

I recently competed the same thing. It took me awhile to figure out, but this is what ultimately worked.

JS FILE (AJAX Request) --> PHP SHELL SCRIPT --> JS FILE I will refer to them as A, B, and C

A looks something like this:

function verify(){

        $.ajax({
        url:"scripts/shell.php", //the page containing php script
        type: "post", //request type,
        dataType: 'json',
        data: {data1: "hi", data2: "hello", data3: "bye"}, //OPTIONAL, Used as arguments for js file down the road. 
        success:function(result){
         console.log(result.abc);
       }
     });
    }

Pretty standard AJAX request

B Looks like this:

<?php  
$EH= $_POST['data1'];
$EHH = $_POST['data2'];
$EHHH = $_POST['data3'];

echo json_encode(array("abc" => shell_exec("...PATH-TO-JS/file.js 2>&1 $EH $EHHH $EHHH")));
?>

NOTICE This shell script echo 's and encodes the result BACK to the array abc as mentioned at the end of A.

C Looks Like This:

var system = require('system')
var H = system.args[1]
var Hi = system.args[2]
var Hii = system.args[3]
if(1 > 2){
 console.log("Passing Result Back") // OR true
}

That result then gets passed back to the shell, which encodes and passes back to the original js file that called it.

Hope this helps.

Here is PHP code for sending String to Java app and will get a response as string format:

<?php

// The data to send to the API
$postData = array(
    'data' => 'Test'
);
//echo json_encode($postData);

// Create the context for the request
$context = stream_context_create(array(
    'http' => array(

        'method' => 'POST',
        'header' => "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
));


// Send the request
$response = file_get_contents('http://localhost:8080/EDI271Parser/rest/ediresponceback/', FALSE, $context);

// Check for errors
if($response === FALSE){
    die('Error');
}


// Print the date from the response
echo $response;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM