Vignesh Prajapati
Vignesh Prajapati

Reputation: 2350

want to send and receive json data in php

As per application requirement, I am trying to develop two PHP which can communicate with each other via Json. I tried searching online but didn't found solution.

Can any one suggest me the right path for this?

I have data in mysql database, the converted data will be in json format as given below: (Also looking for code to get this data format via PHP-JSON object and array.)

{ "user" : [   
             { "firstName" : "Vignesh",  
               "lastName"  : "Prajapati",
               "age"       : 23,
               "email"     : ["[email protected]","[email protected]"],
               "subject"   : ["English","Gujarati", "Hindi"]
             }, 

             { "firstName" : "Vaibhav",  
               "lastName"  : "Prajapati",
               "age"       : 19,
               "email"     : ["[email protected]","[email protected]","[email protected]"],
               "subject"   : ["English","Spanish", "Chinese","Sanskrit"]
             }
           ]
} 

My Php code for sending Json data: (send.php)

<?php

$data = '    { "user" : [   
                 { "firstName" : "Vignesh",  
                   "lastName"  : "Prajapati",
                   "age"       : 23,
                   "email"     : ["[email protected]","[email protected]"],
                   "subject"   : ["English","Gujarati", "Hindi"]
                 }, 

                 { "firstName" : "Vaibhav",  
                   "lastName"  : "Prajapati",
                   "age"       : 19,
                   "email"     : ["[email protected]","[email protected]","[email protected]"],
                   "subject"   : ["English","Spanish", "Chinese","Sanskrit"]
                 }
               ]
    } ';

$url_send ="http://localhost/rec.php";
$str_data = json_encode($data);

function sendPostData($url, $post){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  $result = curl_exec($ch);
  curl_close($ch);  // Seems like good practice
  return $result;
}

echo " " . sendPostData($url_send, $str_data);

?>

My Php code for receiving Json data: (rec.php)

<?php

$json_input_data=json_decode(file_get_contents('php://input'),TRUE);

echo $json_input_data;


?>

Upvotes: 4

Views: 8637

Answers (3)

Madhurendra Sachan
Madhurendra Sachan

Reputation: 760

Modify your function to send header of JSON_DATA in post request

function sendPostData($url, $post){
 $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($post))                                                                       
   );  
  $result = curl_exec($ch);
  curl_close($ch);  // Seems like good practice
  return $result;
}

In file use

<?php

$json_input_data=json_decode(file_get_contents('php://input'),TRUE);

print_r( $json_input_data);


?>

As everyone said there is no need of $str_data = json_encode($data);,Since data is already in json.

Upvotes: 2

Cyclonecode
Cyclonecode

Reputation: 29991

Since you shouldn't use json_encode if the data already is in json format, change your code to something like:

$data = array('user' => array(
         array('firstName' => 'Vignesh', 'lastName' => 'Prajapati'),
         array('firstName' => 'Vaibhav', 'lastName' => 'Prajapati')
       ));

Of course you will need to add the other fields to the array as well.

Using json_encode() on the above data will return:

{
   "user" : [
     {
      "firstName" : "Vignesh",
      "lastName" : "Prajapati"
     },
     {
      "firstName" : "Vaibhav",
      "lastName" : "Prajapati"
     }
   ]
 }

Upvotes: 1

Josh from Qaribou
Josh from Qaribou

Reputation: 6898

Remember that JSON means 'object notation', ie it's a way to describe an object in javascript. It's a great way to communicate at the network-level, but when you're in PHP you should be using the data structures that PHP is designed to use. Rather than work with JSON directly on either side, and especially as an alternative to a big JSON string, keep that data structured as an array and encode it right before sending. Your approach with curl is fine, though a bit custom - there's lots of nice routers that do a better job managing these sorts of requests (symfony is my favourite), but that's a separate issue.

e.g. instead of your big string, represent it as:

$data = 
    [ "user" => [   
                 [ "firstName" => "Vignesh",  
                   "lastName"  => "Prajapati",
                   "age"       => 23,
                   "email"     => ["[email protected]","[email protected]"],
                   "subject"   => ["English","Gujarati", "Hindi"]
                 ]

etc.. When it comes time to send it to the other server, json_encode and go.

Upvotes: 1

Related Questions