简体   繁体   English

在PHP中处理cURL

[英]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. 我正在尝试将一个登录Web界面连接到我在AWS上已创建的身份验证活动存储库。 I am new on that, I have tried doing it using Postman, with a raw template for a JSON application like this: 我是新手,我尝试使用Postman,使用像这样的JSON应用程序的原始模板:

{
"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. 但是当我使用我的php项目执行此操作时,我使用相同的确切数据填充表单时,我收到状态:403消息:用户名或密码错误。

The Postman code, offers me that solution for the CURLOPT_POSTFIELDS: Postman代码,为我提供了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. 我注意到在我的身份验证存储库中,我有JSON格式的访问数据,而我没有以JSON格式发送数据。 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 . 我把阵列中的CURLOPT_POSTFIELDS和改变“内容类型:应用”到“内容类型:application / JSON”,在CURLOPT_HTTPHEADER

Doing that fixed the problem. 这样做解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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