我在PHP中使用HttpURLConnection来调用Java文件。在 JAVA 中如何返回字段内容(字符串(,在 PHP 中如何接收它们?谢谢。对任何可以提供帮助的人都很棒。哈哈。
示例代码:
爪哇岛:
@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>
我最近参加了同样的事情。我花了一段时间才弄清楚,但这就是最终奏效的方法。
JS FILE (AJAX Request) --> PHP SHELL SCRIPT --> JS FILE
我将它们称为 A、B 和 C
A 看起来像这样:
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);
}
});
}
相当标准的AJAX
要求
B 看起来像这样:
<?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")));
?>
注意 这个 shell 脚本echo
的,并将结果encodes
回数组abc
如 A 末尾所述。
C 看起来像这样:
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
}
然后,该结果将传递回 shell,后者进行编码并传递回调用它的原始 js 文件。
希望这有帮助。
以下是用于将字符串发送到 Java 应用程序的 PHP 代码,并将获得字符串格式的响应:
<?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/jsonrn",
'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;
?>