I’m making an webapp. In this webapp im using a webservice that was already made. Before i started to develop this app for the web , i make the exact same in java for android and everything of the post requests worked fine but in this web app it doesen’t. Also in postman works like a charm.
Can someone help me to figure out thats wrong?
Here is my code:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
session_start();
$totalPrice = 0;
$itemTotal = 0;
if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
echo '<ul>';
foreach ($_SESSION['cart'] as $key => $item) {
echo '<li>';
// Output item details
// For example:
echo 'Código: ' . $item['codigo'] . ', ';
echo 'Descrição: ' . $item['descricao'] . ', ';
echo 'Preço: ' . $item['preco'] . ', ';
echo 'Quantidade: ' . $item['quantidade'];
echo '</li>';
}
echo '</ul>';
} else {
echo 'O carrinho está vazio.';
}
echo '<p>Total a pagar: ' . $totalPrice . '€</p>' . '<br>';
if (isset($_POST['fazerEncomenda'])) {
if (!isset($_SESSION['user_id'])) {
echo 'Utilizador não está logado.';
exit;
}
$userId = $_SESSION['user_id'];
// THE PARAMETERES ARE FIXED FOR NOW
$order = array(
"FiscalYear" => "2024",
"SectionCode" => "1",
"DocTypeAbbrev" => "ENCCL",
"EntityCode" => 1,
"Date" => "09/02/2024",
"ExpirationDate" => "08/12/2024",
"CurrencyCode" => "EUR",
"Lines" => array(
array(
"LineNumber" => 1,
"ItemCode" => "1",
"ItemDescription" => "artigo teste",
"Quantity" => 1,
"VATTax" => 23,
"UnitPriceExcludedVAT" => "1",
"GetReportBytes" => true
)
)
);
$ch = curl_init('http://myip/ERPV22/api/EncomendarUser/GenerateCustomerOrder');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Cookie: PHPSESSID=' . session_id()
));
$decodedCookies = urldecode($_SERVER['HTTP_COOKIE']);
curl_setopt($ch, CURLOPT_COOKIE, $decodedCookies);
$response = curl_exec($ch);
echo $response;
}
?>
<form method="post">
<input type="submit" name="fazerEncomenda" value="Fazer Encomenda">
</form>
Thanks for everyone’s help!