Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

willmkt's avatar

Guzzle Sending 2 requests on same time Ask Question

I'm trying to make 2 api calls at the same time, the first gets a token and the second brings the user data.

The problem is that when i pass the token on the second api call it gives me a session expired.

$client = new Client([
    'base_uri' => 'https://portaldoagente.com.br/WCF/wcfTravellink/Loja.svc/', 
    'headers' => [
        'Content-Type' => 'application/json'
        ]
    ]);

First call:

$response = $client->post('Autenticar',
    ['body' => json_encode(
        [
            'login' => 'example',
            'senha' => 'example'
        ]
    )],
);
    $body = $response->getBody()->getContents();
    $responseXml = simplexml_load_string($body);

    $token = $responseXml->children;

Second call

    $response = $client->post('ConsultarCliente',
        ['body' => json_encode(
            [
                'token'=> $token,
                'ClienteLogin' => [
                    'Login' => 'example',
                    'Senha' => 'example'
                ]
            ]
        )]
    );

    $Consulta = $response->getBody()->getContents();

The token needed to stay in the session so it would not expire? When I try this in postman it works fine, first I make a call to the first method to get the token, then I make another call to the second method passing the token.

0 likes
5 replies
robrogers3's avatar

umm, assuming the calls come from a request, where are you storing the $token.

I don't see any reference to session.

willmkt's avatar

I changed the code, but still does not work

protected function obtainAccessToken(Request $request)
{

    $client = new Client([
        'curl' => [CURLOPT_CAINFO => base_path('resources/certs/cacert.pem')],            
        'base_uri' => 'https://portaldoagente.com.br/WCF/wcfTravellink/Loja.svc/', 
        'headers' => [
            'Content-Type' => 'application/json'
            ]
        ]);

    $response = $client->post('Autenticar',
        ['body' => json_encode(
            [
                'login' => 'example',
                'senha' => 'example'
            ]
        )]
    );
        $body = $response->getBody()->getContents();
        $responseXml = simplexml_load_string($body);
        $token = $responseXml->children;
        $request->session()->put('token', $token);
        return $token;

}

protected function consultaCliente(Request $request)
{
        $token = $this->obtainAccessToken($request);
        $value = $request->session()->get('token');        
        
        $client = new Client([
        'curl' => [CURLOPT_CAINFO => base_path('resources/certs/cacert.pem')],                        
        'base_uri' => 'https://portaldoagente.com.br/WCF/wcfTravellink/Loja.svc/', 
        'headers' => [
            'Content-Type' => 'application/json'
            ]
        ]);

        $response = $client->post('ConsultarCliente',
            ['body' => json_encode(
                [
                    'token'=> $value,
                    'ClienteLogin' => [
                        'Login' => 'example',
                        'Senha' => 'example'
                    ]
                ]
            )]
        );

        $ConsultaWooba = $response->getBody()->getContents();
         return $ConsultaWooba;

}
willmkt's avatar

The first call you get a token The second call you pass the token with a client information, so you can get all details of the client.

There other methods to update and list client orders but all the requets must pass the token.

The first call works fine, but on the second it gives me session expired.

It was supposed to work.

Any ideas? anyone?

robrogers3's avatar

I don't see any reference to session!.

where are you storing the token for the second call?

or are you doing the calls one after the other?

Please or to participate in this conversation.