当前位置: 动力学知识库 > 问答 > 编程问答 >

How to retrieve JSON data in PHP posted from terminal using CURL

问题描述:

I am using CURL installed in ubuntu to send JSON to PHP page.There in that page I'm trying to decode JSON data and store into DB.

Here it the command used to send 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

Output on terminal:

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 page code:

<?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);*/

?>

but the output of page is

/home/ubuntu/workspace/listener.php:11: string(0) "" hello world/home/ubuntu/workspace/listener.php:14: NULL

Now how do I store JSON data?

网友答案:

I dont know what you are trying to achieve. But from your question, I am guessing that

  1. You are making the cURL call from CLI (command line/terminal) with the JSON encoded data. Then you are processing the data.
  2. You want that data to be somehow available when you hit the file through a webserver.

If that is the case then, you need to store the data in a persistant storage like a flat file in the most basic sense or a database like MySQL as per the commented out code, and then retrieve the same when needed, specially since php is stateless, every call is a new call for it.

Shall update the answer further once I get more clarity on what exactly is your problem.

网友答案:

Try my source. It's working well for me!!

$data_string = '{"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"}}}';

$ch = curl_init('https://phpserver-chaturasan.c9users.io/listener.php');
$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks
);
curl_setopt_array( $ch, $options );                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                      
    'Content-Length: ' . strlen($data_string)));                                                                 
$result = curl_exec($ch);
echo $result;

My result

分享给朋友:
您可能感兴趣的文章:
随机阅读: