PHP cURL processing

I am trying to connect one web login interface to an already created active authentication repository that I have on AWS. I'm new to this, I've tried doing it with Postman, with a raw template for a JSON app like this:

{
"userName" : "Victor",
"userPassword" : "pwd123"
}

      

And it works correctly and I am getting the correct answer from my repository. But when I use my php project to do this and I fill out a form with exactly the same data, I get the status: message 403: wrong username or password.

Postman code, offers me this solution for CURLOPT_POSTFIELDS:

CURLOPT_POSTFIELDS => "{\n\t\"userName\" : \"Victor\",\n\t\"userPassword\" : \"pwd123\"\n}",

      

But I need to fill in these fields with a form so that I can change them as I show you.

Here is the code I am using, im really stuck so i would appreciate any help :)

public function loginAction(Request $request)
{
    $twigParams=array();
    $DTO = new LoginDTO($request, $this->container, null);
    $form = $DTO->getForm();
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {

        $upass = $DTO->getUserPass();
        $uname = $DTO->getUserName();

        $curl = curl_init();

        $fields=array("userName"=> $uname, "userPassword"=> $upass);
        curl_setopt_array($curl, array(
            CURLOPT_PORT => "80",
            CURLOPT_URL => "http://wt-services-internal-appl-blablabla..",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $fields ,
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application",
                ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

curl_close($curl);

$twigParams["form"]=$form->createView();
return $this->render('auth/login.html.twig', $twigParams);
}

      

+3


source to share


1 answer


I noticed that in my authentication repository I have JSON access data and I was not sending JSON data. To fix this, I did the following:

$fields = array("userName" => $uname, "userPassword" => $upass);
$fields = json_encode($fields);

      



I put this array in CURLOPT_POSTFIELDS

and changed "content-type: application" to "content-type: application / json" in CURLOPT_HTTPHEADER

.

Fulfilling this problem.

0


source







All Articles