title: Reading JSON POST using PHP
body: <p>I looked around a lot before posting this question so my apologies if it is on another post and this is only my second quesiton on here so apologies if I don't format this question correctly.</p>
  
  <p>I have a really simple web service that I have created that needs to take post values and return a JSON encoded array. That all worked fine until I was told I would need to post the form data with a content-type of application/json. Since then I cannot return any values from the web service and it is definitely something to do with how I am filtering their post values.</p>
  
  <p>Basically in my local setup I have created a test page that does the following -</p>
  
  <pre><code>$curl = curl_init();
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                                                                  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
      'Content-Type: application/json',                                                                                
      'Content-Length: ' . strlen($data))                                                                       
  );
  curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/');  // Set the url path we want to call
  $result = curl_exec($curl);
  
  //see the results
  $json=json_decode($result,true);
  curl_close($curl);
  print_r($json);
  </code></pre>
  
  <p>On the webservice I have this (I have stripped out some of the functions) -</p>
  
  <pre><code>&lt;?php
  
  header('Content-type: application/json');
  
  /* connect to the db */
  $link = mysql_connect('localhost','root','root') or die('Cannot connect to the DB');
  mysql_select_db('webservice',$link) or die('Cannot select the DB');
  
  if(isset($_POST['action']) &amp;&amp; $_POST['action'] == 'login') {
      $statusCode = array('statusCode'=&gt;1, 'statusDescription'=&gt;'Login Process - Fail');
      $posts[] = array('status'=&gt;$statusCode);
      header('Content-type: application/json');
      echo json_encode($posts);
  
      /* disconnect from the db */
  }
  @mysql_close($link);
  
  ?&gt;
  </code></pre>
  
  <p>Basically I know that it is due to the $_POST values not being set but I can't find what I need to put instead of the $_POST. I tried 
  json_decode($_POST), file_get_contents("php://input") and a number of other ways but I was shooting in the dark a bit.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks, Steve</p>
  
  <p>Thanks Michael for the help, that was a definite step forward I now have at least got a repsonse when I echo the post....even if it is null </p>
  
  <p>updated CURL -</p>
  
  <pre><code>  $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/');  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
  </code></pre>
  
  <p>updated php on the page that the data is posted to -</p>
  
  <pre><code>$inputJSON = file_get_contents('php://input');
  $input= json_decode( $inputJSON, TRUE ); //convert JSON into array
  
  print_r(json_encode($input));
  </code></pre>
  
  <p>As I say at least I see a response now wheras prior it was returning a blank page</p>