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

aurorame's avatar

send post with cookie from controller?

How i can send post request with cookie from controller? I read documentation and can`t find example with post and cookie. Need smth like this, but with cookie or via Guzzle

$response = Http::post($url, [
            'data' => $request->input('data')
        ]);
0 likes
13 replies
Sinnbeck's avatar

@aurorame Untested, but I updated the example.

Curious.: Why do you need to pass cookies?

aurorame's avatar

@Sinnbeck in your example u set cookie from $request, but I need put $request in post data and phpsessid in cookie. I need to make request to another api where auth via PHPSESSID cookie. I can do it via CURL, but i think i can do it with laravel options

aurorame's avatar

@Sinnbeck

function sendCURL($url, $cookie = null, $post) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, lice Gecko) Chrome/58.0.3029.110 YaBrowser/17.6.1.744 Yowser/2.5 Safari/537.36");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
};

to use:

$response = sendCURL($url, "PHPSESSID=...", $post);
Sinnbeck's avatar

@aurorame So you want something like this? You can use the example by @tykus to clean it up :)

$cookieJar = CookieJar::fromArray([
     'PHPSESSID' => '...',
]);
aurorame's avatar

@Sinnbeck it doesn`t work, but my solution with curl above work fine

        $response = Http::withCookies(['PHPSESSID' => '...'], $url)
            ->post($url, [
            'data' => $request->input('data')
        ]);
        echo $response;
aurorame's avatar

@tykus it doesn`t work, but my solution with curl above work fine

        $response = Http::withCookies(['PHPSESSID' => '...'], $url)
            ->post($url, [
            'data' => $request->input('data')
        ]);
        echo $response;
tykus's avatar

@aurorame it doesn`t work??? What error message / response are you getting?

aurorame's avatar

@tykus error that mean cookies not sended, receive access error from api. But i repeate, my solution with curl above work fine

b-rian's avatar

@aurorame I know this is too late, but google led me here, and this could possibly help others. What fixed it for me was making sure I remove 'http://' from the beginning of my cookies domain.

$url = 'http://192.168.1.10';
$domain = '192.168.1.10'; // Make sure to use the correct domain for cookie (no http//).

$response = Http::withCookies(['PHPSESSID' => '...'], $domain) // notice not using $url here
            ->post($url . "/some-endpoint", [
            'data' => $request->input('data')
        ]);
        echo $response;
1 like

Please or to participate in this conversation.