Home > database >  How to pass user input to an api endpoint using php curl?
How to pass user input to an api endpoint using php curl?

Time:09-05

I am trying to POST HTML form login credentials(email & password) to an endpoint using PHP cURL and subsequently redirect to a different page(admin/index.php) after a successful login. However, I keep getting {"success":"false","message":"please provide email and password"} error from my nodejs login endpoint. I am inexperienced with cURL hence I am failing to notice where I am getting it wrong. Please help.

<?php
session_start();
// include('includes/config.php');
$error = ''; //Variable to Store error message;
if (isset($_POST['login'])) {
if (empty($_POST['email']) || empty($_POST['password'])) {
    $error = "Email or Password is Invalid";
} else {
    //Define $user and $pass
    $email = ($_POST['email']);
    $password = ($_POST['password']);

            
    $url = 'http://localhost:5000/auth/login';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERPWD, $email.":".$password);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['email' => $email, 'password' =>$password]));

    $result = curl_exec($curl);
    echo $result;


    if ($result == 200) {

        $_SESSION['alogin'] = $email;
        header("Location: admin/index.php"); // Redirecting to other page
    } else {
        $error = "Try to login.";
    }
    curl_close($curl);
}
}

?>

Here is the HTML form.

<div >
                        <div >
                            LOGIN FORM
                        </div>
                        <div >
                            <form role="form" method="post" name="login" action="index.php" role="form">
                                <div >
                                    <label>Enter Email</label>
                                    <input  type="email" name="email" autocomplete="off" />
                                </div>
                                <div >
                                    <label>Password</label>
                                    <input  type="password" name="password" autocomplete="off" />
                                </div>
                                <button type="submit" name="login" >LOGIN</button>
                            </form>
                        </div>
                    </div>

CodePudding user response:

 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['email' => $email, 'password' =>$password]));

There are two formats for CURLOPT_POSTFIELDS Neither is JSON.

$post = 'key1=value1&key2=value2&key3=value3';
$post = array('key1'=>$value1,'key2'=>$value2,'key3'=>'value3');

You may need to use urlencode() on username and password. Were you told to use the CURLOPT_USERPWD,
That is unconventional.

And this is just wrong. It might work, but it is still wrong.

if ($result == 200) { 

Use

$status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if ($status == 200){...
  • Related