I have a proxy container that makes a curl request to an api container. Below is the curl request for the proxy container:
$postRequest = [
'email' => $_POST['email'],
'password' => $_POST['password']
];
pretty_var_dump($postRequest);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api:80');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_request);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $post_request);
$curl_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($curl_response, true);
pretty_var_dump($response);
This is the code for the api file:
<?php
// return api results
header('Content-Type: application/json; charset=utf-8');
include_once('functions.php');
include_once('config.php');
$conn = new mysqli($host, $user, $pass, $mydatabase);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected to MySQL server successfully!";
}
$email = $_POST['email'];
$signin_password = $_POST['password'];
$select_query = "SELECT user_password, user_id, user_email, user_status FROM user_login WHERE user_email = '{$email}'";
echo $select_query;
$result = $conn->query($select_query);
if (isset($result)) {
$result = $result->fetch_all(MYSQLI_ASSOC);
pretty_var_dump($result);
if (isset($result[0])) {
$result = $result[0];
if (isset($result['user_password']) && $result['user_password']) {
if ($result['user_password']) {
if (isset($result['user_status']) && $result['user_status'] === "1" && isset($result['user_id'])) {
$_SESSION['loggedIn'] = $result['user_status'];
response(['user_id' => $result['user_id'], 'session' => $result['user_status']], 200, 'Sign in successful');
} elseif (isset($result['user_status']) && $result['user_status'] === "2" && isset($result['user_id'])) {
$_SESSION['loggedIn'] = $result['user_status'];
response(['user_id' => $result['user_id'], 'session' => $result['user_status']], 200, 'Sign in successful');
}
} else {
$_SESSION['loggedIn'] = false;
response('N/A', 401, 'Password incorrect');
}
}
} else {
response('N/A', 401, 'Email address not found');
}
}
For some reason the api file isn't reading the POST data from curl and so every time returns null. MYSQLI, curl have both been installed, and if the API file is manually passed correct usernames and passwords it works perfectly.
Any help would be much appreciated!
EDIT I have updated my code for the curl request however i am still recieving null values back. The api code remains unchanged. Is there something obvious here i am missing?
$email = $_POST['email'];
$password = $_POST['password'];
// $data = http_build_query($postRequest);
$data = '{"email":"'.$email.'", "password":"'.$password.'"}';
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
);
pretty_var_dump($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:86');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($curl_response, true);
pretty_var_dump($response);