1

I have been trying to use JSON in my iPhone app to retrieve information from my SQL database with the use of PHP. I can successfully send a JSON and return it to the iPhone app. My problem occurs when I try to take the parameters in from the JSON array that was sent. It fails and I am not sure what is wrong with my statements. Any suggestions would be great.

function connect($config) {
   try {
     $conn = new \PDO('mysql:host=localhost;dbname=authorization',
                    $config['username'],
                    $config['password']);

     $conn->setAttribute(\PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     return $conn;              

  }catch(Exception $e){
    return false;
  }
}

function query($conn, $body) {

if (isset($body['authorization'])) {

        // Put parameters into local variables
        $authorization_code = $body['authorize'];
        $device_id = $body['device'];
    }
$stmt = $conn->prepare("SELECT uses_remaining FROM phone_authorization WHERE authorization_code = $authorization_code");
$stmt->execute();
$results = $stmt->fetchAll();
return $results ? $results : false;
}

if ($_SERVER['HTTP_METHOD'] === 'postValues'){ 

    $body;

    if($_POST == null){

        $handle  = fopen('php://input', 'r');
        $rawData = fgets($handle);
        $body = json_decode($rawData);
    }
    else{
        $body == $_POST;
    }

    $conn = connect($config);

    $row = query($conn, $body);

    if ($row) {
        echo json_encode($row);
    }
    else
    {
        $data['value1'] = $row;
        $data['value2'] = "test";
        echo json_encode($data);
    }

}
else {

    $data['error'] = 'The Service you asked for was not recognized';
    echo json_encode($data);
}

Thanks

1 Answer 1

0

If your $body is json variable you have to decode it to php Use json_decode

function query($conn, $body) {

if (isset($body['authorization'])) {

        // Put parameters into local variables

        $authorization_code =json_decode($body['authorize'], true);
        $device_id = json_decode($body['device'], true);
    }
$stmt = $conn->prepare("SELECT uses_remaining FROM phone_authorization WHERE authorization_code = $authorization_code");
$stmt->execute();
$results = $stmt->fetchAll();
return $results ? $results : false;
}

if ($_SERVER['HTTP_METHOD'] === 'postValues'){ 

    $body;

    if($_POST == null){

        $handle  = fopen('php://input', 'r');
        $rawData = fgets($handle);
        $body = json_decode($rawData);
    }
    else{
        $body == $_POST;
    }

    $conn = connect($config);

    $row = query($conn, $body);

    if ($row) {
        echo json_encode($row);
    }
    else
    {
        $data['value1'] = $row;
        $data['value2'] = "test";
        echo json_encode($data);
    }

}
else {

    $data['error'] = 'The Service you asked for was not recognized';
    echo json_encode($data);
}
Sign up to request clarification or add additional context in comments.

5 Comments

tell me if this help you or not
So this didn't help any. I did test how my if($_POST == null) was working and it does appear the $_POST is null. So now why wouldn't I be able to access the contents of $body? I thought after decoding I could just $content = $body['authorize']. I assign the JSON in my app with a key value pair like so: [dictionary setValue:code forKey:@"authorize"];
Ok this is what I was doing wrong it appears my notation is wrong. Even when you decode a json it is still in object notation. So if I do this $body->{'authorization'} it will work. Answer from stackoverflow.com/questions/9192602/php-access-json-decode-data
use $json = file_get_contents('php://input'); $data = json_decode($json, true); to get the data from request
If you are saying to replace what is inside of if($_POST == null){ with that it doesn't seem to do anything(except reduce a line of code). Unless I have misunderstood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.