我使用安裝在Ubuntu的CURL發送JSON到PHP page.There在那個頁面我試圖解碼JSON數據並存儲到數據庫。如何檢索從終端發佈的PHP使用CURL JSON數據
在這裏它用於發送JSON
curl -i -X POST -d '{"Customer":{"first_name":"First name","last_name":"last name","email":"[email protected]","addresses":{"address1":"some address","city":"city","country":"CA","first_name":"Mother","last_name":"Lastnameson","phone":"555-1212","province":"ON","zip":"123 ABC"}}}' https://phpserver-chaturasan.c9users.io/listener.php
輸出上終端的命令:
HTTP/1.1 200 OK
date: Tue, 19 Jul 2016 05:32:25 GMT
server: Apache/2.4.7 (Ubuntu)
x-powered-by: PHP/5.5.9-1ubuntu4.17
set-cookie: XDEBUG_SESSION=cloud9ide; expires=Tue, 19-Jul-2016 06:32:25 GMT; Max-Age=3600; path=/
set-cookie: XDEBUG_SESSION=cloud9ide; expires=Tue, 19-Jul-2016 06:32:25 GMT; Max-Age=3600; path=/
vary: Accept-Encoding
content-length: 947
keep-alive: timeout=5, max=100
content-type: text/html
X-BACKEND: apps-proxy
/home/ubuntu/workspace/listener.php:11:
string(258) "{"Customer":{"first_name":"First name","last_name":"last name","email":"[email protected]","addresses":{"address1":"some address","city":"city","country":"CA","first_name":"Mother","last_name":"Lastnameson","phone":"555-1212","province":"ON","zip":"123 ABC"}}}"
hello world/home/ubuntu/workspace/listener.php:14:
array(1) {
'Customer' =>
array(4) {
'first_name' =>
string(10) "First name"
'last_name' =>
string(9) "last name"
'email' =>
string(15) "[email protected]"
'addresses' =>
array(8) {
'address1' =>
string(12) "some address"
'city' =>
string(4) "city"
'country' =>
string(2) "CA"
'first_name' =>
string(6) "Mother"
'last_name' =>
string(11) "Lastnameson"
'phone' =>
string(8) "555-1212"
'province' =>
string(2) "ON"
'zip' =>
string(7) "123 ABC"
}
}
}
PHP頁代碼:
<?php
ini_set("allow_url_fopen", true);
/*if(isset($_POST['Customer'])){
$db = new mysqli("localhost","root","","gen");
$name = $db->real_escape_string($_POST['Scenes']);
$query = "INSERT INTO g SET name = '$name'";
$db->query($query);
}
*/
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
var_dump($jsonStr);
echo "hello world";
$json = json_decode($jsonStr, true);
var_dump($json);
/*db = new mysqli("localhost","root","","gen");
$query = "INSERT INTO g SET name = '$json'";
$db->query($query);*/
?>
但頁面的輸出是
/home/ubuntu/workspace/listener.php:11: string(0) "" hello world/home/ubuntu/workspace/listener.php:14: NULL
現在我該如何存儲JSON數據?
謝謝你的答案我將json文件直接存儲到mysql – Chaturasan