Receiving HTTP POST Json with PHP MYSQL PDO

Asked

Viewed 436 times

0

I’m having a hard time solving a problem and I’m counting on your help. I am receiving, from an external API, an HTTP POST request in JSON format with some information. I need to take this information separately and do a MYSQL UPDATE 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 bank:

<?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 realized that before going through json_decode is 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 get from PHP is the following: PHP Notice: Trying to get Property of non-object. Right in these lines:

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

I var_dump and the following appears:

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

It seems that you are not passing to an Array correctly:

Thank you in advance.

2 answers

1

To try to clarify what @rcs answered:

Without the second parameter json_decode($json_str):

$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);
  • Thank you @Andre Felipe Azevedo ... Now it worked ...

0

The second parameter for json_decode

The problem is that the function json_decode is receiving true as the second parameter, this causes an array to be returned and not an object. In that case, you would have to take the amount using $obj['quantity']. Or change the code to not pass true as the second parameter of the function to be able to access as object with the syntax $obj->quantity.

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

$quantity = $obj['quantity'];

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

$quantity = $obj->quantity;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.