I have my ubuntu 18.04 server with apache2. I am trying to insert data from json file into my mysql database using curl POST. Curl will invokes php script (my-parser.php) passing json file. The php script will parse json, create a sql query and insert the data into db.
Now Curl returns error (below). Any help will be appreciate
My Curl:
curl -vv --header 'Content-Type: application/json; charset=UTF-8' --data
@file_with_json_data http://localhost/php-parser.php
Curl response:
Trying 127.0.0.1
Connected to localhost port 80
POST /php-parser.php HTTP/1.1
HOST:localhost
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json; charset=UTF-8
Content-Length: 210
*upload completely sent off: 210 of 210 bytes
*HTTP/1.0 assume close after body
HTTP/1.0 500 Internal Server Error
Server: Apache/2.4.29 (Ubuntu)
Content-Length:0
Connection: close
Content-Type: text/html; charset=UTF-8
I have tried to find answer on stackoverflow but could not find the solution.
My MySQL database is My_DB, my table in it is My_Data_Table. The table has 4 columns:
index (auto_increment)
mcc TEXT,
mcc TEXT,
lac TEXT
My file_with_json_data:
{
"event": "some_event:,
"data": {
"MCC":"111",
"MNC":"123",
"LAC":"345
},
"num1":60,
"string":"sometext",
"num2:"123"
}
My php-parser.php:
<?php
$json=$_POST;
$data=json_decode($json, true);
$hostname="localhost";
$username="user";
$password="password";
$db="My_DB";
$dbconnect=mysqli_connect($hostname, $username, $password, $db);
if ($dbconnect->connect_error) {
die("Database connection failed:" . $dbconnect->connect_error);
}
$mcc=$data["MCC"];
$mnc=$data["MNC"];
$lac=$data["LAC"];
$query='INSERT INTO My_Data_Table (mcc, mnc, lac)
VALUE ("$mcc", "$mnc", "$lac")';
if (!mysqli_query($dbconnect, $query)){
die('An error occurred.');
}else{
echo "Success!";
}
mysqli_close($dbconnect);
?>
I expect that curl will invoke php script which inserts the json data into mysql table in specified columns. Curl verbose reply shows
Server Error 500 HTTP/1.0 assume close after body