我想将数据发送到具有json格式的Web服务,如下所示:
{
"request":
{
"t_sep":
{
"noKartu":"0000011110116",
"tglSep":"2016-06-12 09:00:00",
"noRujukan":"00001",
"catatan":"test"
}
}
}
通过以下回复(回复):
{
metadata:
{
code: "200"
message: "OK"
}
response: "0301R00105160000123"
}
我想创建一个表单来更改变量的值" noKartu"," tglSep"," noRujukan"和" catatan"。
formulir.php
<form id="form1" name="form1" method="post" action="aksiformulir.php">
no kartu: <input name="noKartu" type="text" size="25" /><br>
tgl sep: <input name="tglSep" type="text" size="25" /><br>
no rujukan: <input name="noRujukan" type="text" size="25" /><br>
catatan: <input name="catatan" type="text" size="25" /><br>
<input type=submit value=Go>
</form>
和aksiformulir.php
<?php
$dataid = "10000";
$secretKey = "56789";
$localIP = "dvlp.bpjs-kesehatan.go.id";
$url = "http://".$localIP."/devWsLokalRest/SEP/insert";
$port = 8081;
date_default_timezone_set('UTC');
$tStamp = strval(time() - strtotime('1970-01-01 00:00:00'));
$signature = hash_hmac('sha256', $dataid . "&" . $tStamp, $secretKey, true);
$encodedSignature = base64_encode($signature);
$urlencodedSignature = urlencode($encodedSignature);
function post_request($url, $port, $dataid, $tStamp, $encodedSignature, $data, $referer = '')
{
//-Convert the data array into URL Parameters like a=b&foo=bar etc.
//$data = http_build_query($data);
// parse the given URL
$url = parse_url($url);
if ($url['scheme'] != 'http') {
die('Error: Only HTTP request are supported !');
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80 - timeout: 50 sec
$fp = fsockopen($host, $port, $errno, $errstr, 50);
if ($fp) {
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
if ($referer != '')
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "x-cons-id: " . $dataid . "\r\n");
fputs($fp, "x-timestamp: " . $tStamp . "\r\n");
fputs($fp, "x-signature: " . $encodedSignature . "\r\n");
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while (!feof($fp)) {
// receive the results of the request, 128 char
$result .= fgets($fp, 128);
}
} else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// return as structured array:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
$databpjs = '{
"request":
{
"t_sep":
{
"noKartu":"$_POST[noKartu]",
"tglSep":"$_POST[tglSep]",
"noRujukan":"$_POST[noRujukan]",
"catatan":"$_POST[catatan]"
}
}
}';
$data = array(
'Data' => $databpjs
);
$result = post_request($url, $port, $dataid, $tStamp, $encodedSignature, $databpjs, $referer = '');
if ($result['status'] == 'ok') {
//mengubah "re d sponse" menjadi "response"
$resultstr = str_replace("re d sponse", "response", trim(preg_replace('/\s\s+/', ' ', $result['content'])));
// print the result of the whole request:
echo "<pre>";
echo $resultstr;
echo "</pre>";
} else {
echo 'A error occured: ' . $result['error'];
} ?>
但是回应并不像预期的那样。
任何人都可以帮助如何通过表单发送数据?
答案 0 :(得分:0)
我认为问题出在你的$databpjs
。您以字符串形式发送数据,而不是以json格式发送。试试这个。
$databpjs = array("request" =>
array("sep" =>
array(
"noKartu" => "0000011110116",
"tglSep" => "2016-06-12 09:00:00",
"noRujukan" => "00001",
"catatan" => "test"
)
)
);
它将生成有效的json格式。