want to send and receive json data in php

前端 未结 3 2022
滥情空心
滥情空心 2021-02-10 23:34

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

相关标签:
3条回答
  • 2021-02-11 00:16

    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"     => ["vignesh@gmail.com","vignesh@yahoo.com"],
                       "subject"   => ["English","Gujarati", "Hindi"]
                     ]
    

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

    0 讨论(0)
  • 2021-02-11 00:28

    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.

    0 讨论(0)
  • 2021-02-11 00:35

    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"
         }
       ]
     }
    
    0 讨论(0)
提交回复
热议问题