-3

I don't understand this. I'm sending my JSON via C# and my PHP on my MySQL server is catching it but where I usually catch with post I now see I should catch it using json_decode but even through I can see the data and print it out.

I cannot seem to get it in to a usable format to insert into my database:

Array print out using print_r ($data); looks like this 

/Array
(
    [widget1] => 54
     [widget2] => tttt
    [widget3] => tttttttt
   [widget4] => 100
   [widget5] => 3
    [imageobject] => /9j/4QPqRXhpZgAATU0AKgAAAAgADAEbAAUAAAABAAAAngEaAAUAAAABAAAApgEAAAQAAAABAAAFUwEQAAIAAAAJAAAArgExAAIAAAAOAAAAtwEPAAIAAAAIAAAAxQEBAAQAAAABAAAD8wITAAMAAAABAAEAAIdpAAQAAAABAAAA4QESAAMAAAABAAAAAAEoAAMAAAABAAIAAAEyAAIAAAAUAAAAzQAAA3gAAABIAAAAAQAAAEgAAAABU00tRzk1NUYARzk1NUZYWFUyQ1JGNwBzYW1zdW5nADIwMTg6MDc6MTkgMTY6NDc6MjEAACCSAgAFAAAAAQAAAmeQAAAHAAAABDAyMjCSBAAKAAAAAQAAAm+IIgADAAAAAQACAACkIAACAAAAGQAAAnegAQADAAAAAQABAACSBQAFAAAAAQAAApCgAwADAAAAAQH5AACSAwAKAAAAAQAAApiQAwACAAAAFAAAAqCgAAAHAAAABDAxMDCSfAAHAAAAYgAAArSSkQACAAAABQAAAxakAwADAAAAAQAAAACgBQAEAAAAAQAAA2akAgADAAAAAQAAAACCmgAFAAAAAQAAAxuSCQADAAAAAQAAAACSkAACAAAABQAAAyOCnQAFAAAAAQAAAyiIJwADAAAA)

and im trying to access the data into variables like below which is obviously wrong

<?php
header('Content-Type: application/json;charset=utf-8');

require "connect.php";


//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; charset=utf-8') != 0){
   throw new Exception('Content type must be: application/json '.$contentType);

}

//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));

//Attempt to decode the incoming RAW post data from JSON.
$data = json_decode($content, true);

//If json_decode failed, the JSON is invalid.
if(!is_array($data)){
    throw new Exception('Received content contained invalid JSON!');
}

print_r ($data);

$Widget1= $data[0]->widget1; 
$Widget2= $data[0]->widget2; 
$Widget3= $data[0]->widget3; 
$Widget4= $data[0]->widget4; 
$Widget5= $data[0]->widget5; 
$Widget6= $data[0]->imageobject; 
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

So Alex was right this solved my issue $Widget1= $data['widget1'];