I've created an API for mySQL database, to get the product data for a given barcode.
It generally works well, I'm able to retrieve all the data with a POST request that has Content-Type application/x-www-form-urlencoded and body parameter for the barcode.
Postman with a barcode parameter and response
However, the issue is when the product for the given barcode contains umlaut(s) - in that case I don't get anything back.
Product that contains umlaut in the name
Postman with that barcode parameter
Also, here's the products.php that I'm using for the request.
// Create connection
$con=mysqli_connect("localhost"," "," "," ");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//
$barcode = $con->real_escape_string($_POST['barcode']);
$stmt = $con->prepare("SELECT * FROM products WHERE UPC = ? OR EAN = ?");
$stmt->bind_param("ss", $barcode, $barcode);
$stmt->execute();
// Check if there are results
if ($result = $stmt->get_result())
{
// Prepares all the results to be encoded in a JSON
$row = $result->fetch_assoc();
$product_upc = $row['UPC'];
$product_ean = $row['EAN'];
$product_asin = $row['ASIN'];
$product_name =$row['name'];
$product_brand =$row['brand'];
$product_category = $row['category'];
$product_image =$row['imageURL'];
$product_ingredients =$row['ingredients'];
$product = array('UPC' => $product_upc, 'EAN' => $product_ean, 'ASIN' => $product_asin, 'name' => $product_name, 'brand'=> $product_brand, 'category'=> $product_category, 'imageURL'=> $product_image, 'ingredients'=> $product_ingredients);
// encodes array with results from database
echo json_encode($product);
}
// Close connections
mysqli_close($con);
?>
(Just to mention that I've tried both with json_encode($product) as well as with json_encode($product, JSON_UNESCAPED_UNICODE) and it's the same)
Any ideas on how I could fix this? Thanks!