1

我想在注册成功时登录用户。我正在使用带有 fos_user 的 Symfony 3。这是注册码:

$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
$user->setUsername($username);
$user->setPlainPassword($password);
$user->setEmail($email);
$role = "ROLE_USER";
$user->setRoles(array($role));
$user->setEnabled(true);
$user->setName($name);
$userManager->updateUser($user);

现在我尝试像这样使用 CURL 登录用户:

//LOGIN THE USER 
$data = array(
      '_username' => $username,
      '_password' => $password,
      '_remember_me' => false
);
$url = $this->generateUrl('fos_user_security_check');
$ch = curl_init($url);
$postString = http_build_query($data, '', '&');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
));
$response = curl_exec($ch);
curl_close($ch);

但是这段代码没有登录用户和 /login_check 操作,让我回到 /login 页面。 在此处输入图像描述 我尝试使用“REST CLIENT”,我可以通过设置内容类型登录用户:application/x-www-form-urlencoded,但我认为 CURL 没有设置内容类型,因为当我打印“curl_getinfo($ch) ;" 返回这个:

[url] => http://192.168.1.100/web/app_dev.php/login_check
[content_type] => text/html; charset=UTF-8
[http_code] => 302
[header_size] => 401
[request_size] => 222
...

我在 "security.yml" 中评论 "csrf_token_generator: security.csrf.token_manager" 以在没有 TOKEN 的情况下登录。谁能帮我?

4

0 回答 0