want to send and receive json data in php

前端 未结 3 2024
滥情空心
滥情空心 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: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

    
    

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

提交回复
热议问题