Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 会话未从Guzzle Post启动_Php_Laravel_Symfony_Session_Guzzle - Fatal编程技术网

Php 会话未从Guzzle Post启动

Php 会话未从Guzzle Post启动,php,laravel,symfony,session,guzzle,Php,Laravel,Symfony,Session,Guzzle,我正在将Laravel集成到一个遗留php应用程序中。登录页面用于直接发布到verifyUser.php,该页面也启动了Symfony会话 新的体系结构现在发布到一个LaravelAPI,该api将Guzzle发布到verifyUser.php javascript: $(document).ready(function(){ $('#signIn').submit(function(){ var a = $('#email').val(); $.post('/api/l

我正在将Laravel集成到一个遗留php应用程序中。登录页面用于直接发布到
verifyUser.php
,该页面也启动了Symfony会话

新的体系结构现在发布到一个LaravelAPI,该api将Guzzle发布到
verifyUser.php

javascript:

$(document).ready(function(){
  $('#signIn').submit(function(){
    var a = $('#email').val();  
    $.post('/api/login', { //this used to post to verifyUser.php
      Username: $('#email').val(),
      Password: $('#password').val()
    }, function(data){
      if(data['credentials'] == true){
          console.log('credentials true');
          console.log(data['uri']);
          window.location.href=data['uri'];
      } else {
        $('#errMsg').html(data['errMsg']);
        $('.alert').show();
      }
    }, 'json');
    return false;
  });
控制器功能:

public function authenticate(Request $request) //aka api/login endpoint
{
    //...
            $legacyRes = $this->authenticateLegacy($request);
    //...
}


private function authenticateLegacy(Request $request)
{
    $response = null;
    try {
        $response = $this->client->post('/admin/verifyUser.php', [
            'form_params' => ['Username' => $request->get('Username'),
                'Password' => $request->get('Password')]
        ]);
    }
    catch(Exception $exception){
        Log::error('Errrererererer', [$exception->getMessage()]);
    }
    $body = (string)$response->getBody();
    Log::info('BODY:', [$body]);
    return $body;

}
我省略了verifyUser.php,因为我已经对它进行了测试,它返回了预期的结果

使用浏览器时,会话信息似乎未设置。但根据我的帖子回复,一切都应该正常。
这是因为我正在通过guzzle路由请求吗?

我假设您的旧端点使用cookies来标识用户的会话

对旧端点的成功请求返回
Set Cookie

Guzzle不会将此
Set Cookie
头从API响应转发到浏览器-您必须将此行为编程到“包装”应用程序中

当发送任何进一步的请求时,您需要告诉guzzle显式地将相应的
Cookie
头传递给遗留api(以维护用户的登录状态)


为了实现这一点,您需要将此cookie保存在新应用程序中(即在用户会话或数据库中),然后在
cookie
标题中传递它,以及您对遗留API的所有进一步请求。

在“我的答案”下发布以显示更新的代码:

private function authenticateLegacy(Request $request)
{
    //...
    //parse cookie id from guzzle response
    $body = (string)$response->getBody();
    $cookie = $response->getHeader('Set-Cookie'); //PHPSESSID=SOMEID; path=/
    $cookieBite = explode(';', $cookie)[0]; ////PHPSESSID=SOMEID
    $cookieId = explode('=', $cookieBite)[1];

    $data = json_decode($body, true);
    $data['session'] = $cookieId;
    return $data;
}
在行动中:

public function authenticate(Request $request)
{
    //...
            $legacyRes = $this->authenticateLegacy($request);
    //...
    // this will have the session id in the body but will also 
    // set the cookie for the client so I don't have 
    // to set document.cookie w/ js
    return response($legacyRes, 200)
     ->withCookie('PHPSESSID', $legacyRes['session']);
}

大家好,欢迎来到stackoverflow。我们确实需要更多的信息来帮助您解决问题。请在您的问题中包含相关的代码部分。谢谢,我已经更新了我的问题。我在guzzle标题中看到了cookie。我会尝试你的解决方案,以确保,但我认为这是我的问题