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.
Thank you @Andre Felipe Azevedo ... Now it worked ...
– Emetec