0

我正在制作一个应用程序,我需要使用 POST cUrl 调用对用户进行身份验证:

$app = new Silex\Application();

$app->register(new Silex\Provider\SessionServiceProvider());

$app['debug'] = true;

$app->post('/api/auth/login', function (Request $request) use ($app) {
    if ($request->get('username') === 'admin' && $request->get('password') === 'admin') {
        return $app->json(array('sessionId' => $app['session']->getId()));
    } else {
        $error = array('message' => 'Authentication failed');
        return $app->json($error, 404);
    }
});

当我使用:

curl -X POST -d username=admin -d password=admin http://app.local/api/auth/login

每次我的应用程序返回一个不同的会话标识符!

4

1 回答 1

1

PHP代码:

<?php

    require_once __DIR__ . '/../vendor/autoload.php';

    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\Request;

    $app = new Silex\Application();
    $app['debug'] = true;

    $app->register(new Silex\Provider\SessionServiceProvider());

    $app->before(function ($request) {
        $request->getSession()->start();
    });

    $app->post('/api/auth/login', function (Request $request) use ($app) {
        if ($request->get('username') === 'admin' && $request->get('password') === 'admin') {
            return $app->json(array('sessionId' => $app['session']->getId()));
        } else {
            $error = array('message' => 'Authentication failed');
            return $app->json($error, 404);
        }
    });

    $app->run();

?>

卷曲命令:

curl -X POST -d username=admin -d password=admin http://app.local/api/auth/login --cookie-jar ./cookie --cookie ./cookie

上述命令将在当前工作目录中写入一个“cookie”文件。请检查目录是否可写或用户具有正确的权限。

于 2013-01-28T09:02:33.163 回答