我正在尝试为我的php网站构建一个API,并且该API正在使用asp.net站点中的数据将数据插入到我的php MySql站点中。我已经与邮递员检查过,它正在工作。但是当从asp.net访问时,它仅获得空值。从asp.net使用JSON发布的数据是
来自 asp.net 的JSON POST 数组
string reqJson = "{\"user_id\":\"" + user_id + "\",\"name\":\"" + name + "\",\"branch\": " + branch + "," + " \"agency\" : \"" + agency + "\"}";
var request = (HttpWebRequest)WebRequest.Create(Url);
var data = Encoding.UTF8.GetBytes(reqJson);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.Accept = "application/json";
request.Headers.Add("Accept-Encoding", "application/gzip");
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
var rsp = webResponse.GetResponseStream();
if (rsp == null)
{
}
using (System.IO.StreamReader rdstrm = new System.IO.StreamReader(webResponse.GetResponseStream()))
{
responseString = rdstrm.ReadToEnd().ToString();
}
这是请求的JSON
{"AllowAutoRedirect":true,"AllowWriteStreamBuffering":true,"AllowReadStreamBuffering":false,"HaveResponse":false,"KeepAlive":true,"Pipelined":true,"PreAuthenticate":false,"UnsafeAuthenticatedConnectionSharing":false,"SendChunked":false,"AutomaticDecompression":0,"MaximumResponseHeadersLength":64,"ClientCertificates":[],"CookieContainer":null,"SupportsCookieContainer":true,"RequestUri":"http://localhost:9080/api/ucd.php","ContentLength":81,"Timeout":100000,"ReadWriteTimeout":300000,"ContinueTimeout":350,"Address":"http://localhost:9080/api/ucd.php","ContinueDelegate":null,"ServicePoint":{"BindIPEndPointDelegate":null,"ConnectionLeaseTimeout":-1,"Address":"http://localhost:9080/api/ucd.php","MaxIdleTime":100000,"UseNagleAlgorithm":true,"ReceiveBufferSize":-1,"Expect100Continue":true,"IdleSince":"\/Date(1542637743629)\/","ProtocolVersion":{"Major":1,"Minor":1,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1},"ConnectionName":"http","ConnectionLimit":2147483647,"CurrentConnections":0,"Certificate":null,"ClientCertificate":null,"SupportsPipelining":true},"Host":"localhost","MaximumAutomaticRedirections":50,"Method":"POST","Credentials":null,"UseDefaultCredentials":false,"ConnectionGroupName":null,"Headers":["Content-Type","Accept","Accept-Encoding"],"Proxy":{"Credentials":null},"ProtocolVersion":{"Major":1,"Minor":1,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1},"ContentType":"application/json","MediaType":null,"TransferEncoding":null,"Connection":null,"Accept":"application/json","Referer":null,"UserAgent":null,"Expect":null,"IfModifiedSince":"\/Date(-62135596800000)\/","Date":"\/Date(-62135596800000)\/","ServerCertificateValidationCallback":null,"CreatorInstance":{},"CachePolicy":{"Level":1},"AuthenticationLevel":1,"ImpersonationLevel":4}
以上是在发布JSON请求时自动生成的
接收 PHP 代码是
<?php
include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
//Posting the data here
$user_id= $_POST['user_id'];
$user_name= $_POST['name'];
$branch= $_POST['branch'];
$agency= $_POST['agency'];
//This below method also tried but the same showing.
/*$portal = isset($_POST['portal']) ? mysqli_real_escape_string($_POST['portal']) : "";
$pax_name = isset($_POST['pax_name']) ? mysqli_real_escape_string($_POST['pax_name']) : "";
$ticket_no = isset($_POST['ticket_no']) ? mysqli_real_escape_string($_POST['ticket_no']) : "";
$adtorch = isset($_POST['adtorch']) ? mysqli_real_escape_string($_POST['adtorch']) : "";
$airline_code = isset($_POST['airline_code']) ? mysqli_real_escape_string($_POST['airline_code']) : "";*/
// Insert data into data base
$sql = "INSERT INTO portal_users (pu_user_id, pu_username, pu_branch, pu_agency) VALUES ('" .$user_id."','".$user_name."','".$branch."','".$agency."');";
$qur = $conn->query($sql);
if($qur){
$json = array("status" => 1, "msg" => $user_id);
}else{
$json = array("status" => 0, "msg" => "Error adding user!");
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}
mysqli_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
?>
收款后是否有任何问题?这是一个json请求。所以我想使用解码方法?我是一个初学者,所以我不知道要解码。
我也尝试过这种方法
$r_val = file_get_contents("php://input");
$r_data = json_decode($r_val, true);
$user_id= $r_data ['user_id'];
$user_name= $r_data ['name'];
$branch= $r_data ['branch'];
$agency= $r_data ['agency'];
答案 0 :(得分:1)
您需要像这样接收json值-
$r_val = file_get_contents("php://input");
$r_data = json_decode($r_val, true);
这里$ r_data是一个数组。您将在下面的数组中获得期望值,如下所示
$user_id= $r_data ['user_id'];
$user_name= $r_data ['name'];
$branch= $r_data ['branch'];
$agency= $r_data ['agency'];
答案 1 :(得分:1)
要在php中接收json POST数据,我们应该使用以下代码
$postdata = file_get_contents("php://input"); //This will post JSON into $postdata as a JSON string
$postdataArray = json_decode($postdata, true); //This will convert JSON string into array
在您的情况下,替换以下代码
//Posting the data here
$user_id= $_POST['user_id'];
$user_name= $_POST['name'];
$branch= $_POST['branch'];
$agency= $_POST['agency'];
使用此代码
$postdata = file_get_contents("php://input");
$postdataArray = json_decode($postdata, true);
$user_id= $postdataArray['user_id'];
$user_name= $postdataArray['name'];
$branch= $postdataArray['branch'];
$agency= $postdataArray['agency'];