简体   繁体   中英

Handling cURL in PHP

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

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

And it works correctly and I receive a right answer from my repository. But when I am using my php project for doing that, and I fill the form with the same exactly data, I receive a status:403 message: Wrong username or password.

The Postman code, offers me that solution for the CURLOPT_POSTFIELDS:

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

But I need to fill this fields with the form so I change them as I will show you.

Here is the code that I am using, im really stuck so I will be thankfull for 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);
}

I noticed that in my authentication repository, I have the access data in a JSON format and I wasn't sending the data in a JSON format. To fix it, I did this:

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

I put that array in the CURLOPT_POSTFIELDS and changed "content-type: application" to "content-type: application/json", in the CURLOPT_HTTPHEADER .

Doing that fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM