Receiving HTTP POST Json with PHP MYSQL PDO

0

I'm having trouble solving a problem and I'm counting on your help. I am getting, from an external API, an HTTP POST request in JSON format with some information. I need to get this information separately and do an UPDATE on MYSQL using PDO. The API sends the information in the following format:

{"type":"info-type","clientId":"info-client","sku":"info-sku","quantity":info-quantity }

I did the following to receive and update the database:

<?php

header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *);

require('../../config.php');

$json_str = file_get_contents("php://input");
$obj = json_decode($json_str, true);

$sku = "$obj->sku";
$quantity = "$obj->quantity";

print_r($obj);

try {
$pdo = new PDO('mysql:host=' . DB_HOSTNAME . ';port=' . DB_PORT . ';dbname=' . DB_DATABASE, DB_USERNAME, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('UPDATE bg_product SET quantity = :quantity WHERE sku = :sku');
$stmt->execute(array(
    ':sku' => $sku,
    ':quantity' => $quantity
    )
);

echo $stmt->rowCount();
}catch(PDOException $e) {
echo 'erro no banco de dados';
}

?>

I noticed that before going through json_decode it looks like this:

"{\"type\":\"info-type\",\"clientId\":\"info-client\",\"sku\":\"info-sku\",\"quantity\":info-quantity}"

And then it shows correctly:

{"type":"info-type","clientId":"info-client","sku":"info-sku","quantity":info-quantity }

The error message I receive from PHP is as follows: PHP Notice: Trying to get property of non-object. Well in this lines:

$sku = "$obj->sku";
$quantity = "$obj->quantity";

I made a var_dump and the following appears:

string(99) "{"type":"info-type","clientId":"info-client","sku":"info-sku","quantity":info-quantity}"

It looks like it's not moving to an Array properly:

Thank you in advance.

    
asked by anonymous 10.07.2018 / 16:52

2 answers

0

To try to clarify what @raphaelcsilva replied:

Without the second json_decode ($ json_str) parameter:

$json_str = '{"type":"info-type","clientId":"info-client","sku":"info-sku","quantity":"info-quantity"}';
$obj = json_decode($json_str);

$sku = $obj->sku;
$quantity = $obj->quantity;

print_r($sku);
print_r($quantity);

With the second parameter json_decode ($ json_str, true)

$json_str = '{"type":"info-type","clientId":"info-client","sku":"info-sku","quantity":"info-quantity"}';
$obj = json_decode($json_str, true);

$sku = $obj['sku'];
$quantity = $obj['quantity'];

print_r($sku);
print_r($quantity);
    
10.07.2018 / 19:58
0

The second parameter for json_decode

The problem is that the json_decode function is expecting true as the second parameter, this causes an array to be returned and not an object. You would have to get the value using $obj['quantity'] . Or, do not pass true as the second parameter of the function to be able to access as object $obj->quantity .

With true as parameter an array is returned, so you access like this:

$quantity = $obj['quantity'];

Without the parameter an object is returned, so you access it like this:

$quantity = $obj->quantity;
    
10.07.2018 / 17:19