php CURL无法设置内容类型

时间:2018-03-05 16:39:48

标签: php symfony curl symfony3.x

我想在注册成功时登录用户。 我正在使用带有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页面。 enter image description here 我尝试使用“REST CLIENT”,我可以通过设置content-type: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的情况下登录。 任何人都可以帮助我吗?

0 个答案:

没有答案
相关问题