title: Send and Receive json data from api
body: <p>I need to send some json data to a url and receive its json response. I am not so good at json, and i searched and found the following code</p>
  
  <pre><code>&lt;?php
  $json_data = array(
  'partnerid' =&gt; '123',
  'outletid' =&gt; '321',
  'mobile' =&gt; '0123456789',
  'secret' =&gt; 'qwert',
  'amount' =&gt; '100',
  );
  
  $json = json_encode($json_data, true);
  
  $req_url = "https://myapiprovider.com/api/requestdata";
  
  //send json request
  //Initiate cURL.
  $ch = curl_init($req_url);
  //Tell cURL that we want to send a POST request.
  curl_setopt($ch, CURLOPT_POST, 1);
  //Attach our encoded JSON string to the POST fields.
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  //Set the content type to application/json
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
  //Execute the request
  $result = curl_exec($ch);
  
  //receive json response
  $content = file_get_contents($req_url);
  //Make sure that it is a POST request.
  if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
  throw new Exception('Request method must be POST!');
  }
  
  //Make sure that the content type of the POST request has been set to application/json
  $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
  if(strcasecmp($contentType, 'application/json') != 0){
  throw new Exception('Content type must be: application/json');
  }
  //Receive the RAW post data.
  $content = trim(file_get_contents("php://input"));
  //Attempt to decode the incoming RAW post data from JSON.
  $decoded = json_decode($content, true);
  //If json_decode failed, the JSON is invalid.
  if(!is_array($decoded)){
  throw new Exception('Received content contained invalid JSON!');
  }
  //Process the JSON.
  ?&gt;
  </code></pre>
  
  <p>I get the following exception 'Content type must be: application/json'. Why? I have set the content type to application/json. right? Please help immidiate.</p>