如何使用Cookie php存储cookie并在进一步的请求中发送它

时间:2019-05-23 08:26:21

标签: php cookies guzzle guzzle6

我在laravel中编写了一个辅助函数,该函数调用可用于cookie会话的api 基本上,要做任何事情,我都必须先调用提供会话cookie的登录api,我需要保存它并在进一步的请求中发送它。

我尝试通过将请求发送到登录api并在cookie jar中获取cookie来解决问题6,然后将cookie jar与其他请求一起发送,这是获取cookie的登录功能

public function loginCookie(array $credentials)
{
    $client = new Client(["cookies"=>true]);

    $response = $client->request(
        'POST',
        "https://origin_url/selectLoginAction.json",
        [
            'form_params' => $credentials,
            'headers' => [
                "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                "Accept-Encoding" => "gzip, deflate, br",
                "Accept-Language" => "en-GB,en-US;q=0.9,en;q=0.8",
                "Cache-Control" => "no-cache",
                "Connection" => "keep-alive",
                "Origin" => "origin_url",
                "Referer" => "https://origi_nurl/selectLoginMain",
                "Upgrade-Insecure-Requests" => "1",
                "User-Agent" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
            ]
        ]
    );

    // dd($response);
    //get the cookie by name
    $cookieconf =$client->getConfig('cookies');
    $cookie =$cookieconf->toArray();

    $cookieArray = [];
    $cookieString = "";

    //for creating a cookie jar in other request we need array in key value form
    foreach ($cookie as $key => $value) {
        if($value["Name"]==="JSESSIONID"||$value["Name"]=="BNES_JSESSIONID"){
            $cookieString .= $value["Name"]."=".$value["Value"]."; ";

        }
        $cookieArray[$value["Name"]]=$value["Value"];

    }
    return $cookieArray;
}

我正在从中获取cookie罐和cookie键值对数组,我尝试将其放入其他客户端对象和请求中,但是没有用,

0 个答案:

没有答案