Jason
Jason

Reputation: 4772

Cannot sent cookie with Guzzle request

I have a PHP script that needs to fetch a CSV file from an application. There is an API for the application that allows the script to lot in, which gives the script a session cookie for authentication. I then need to doa GET request to fetch the CSV file (which the API does not support).

Using curl directory works:

$c = curl_init($url);
curl_setopt($c, CURLOPT_COOKIE, 'PHPSESSID=' . $session_id_from_api);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$csv_file = curl_exec($c);
echo $csv_file;

That fetches the CSV file using the session ID obtained from the API login and passed through a coookie.

Now, I would like to do the same thing using Guzzle, but I just get the login page back instead. This is the code I'm using:

$client = new Guzzle\Http\Client();
$request = $client->get(
    $url,
    [
        'cookies' => ['PHPSESSID' => $session_id_from_api],
    ]
);
$response = $client->send($request);
echo $response->getBody(true);

That gives me the login page, so the GET to the application is not recognising the session defined by the cookie.

Is there anything else I need to do to ensure the cookie and value I specify is sent to the remote application?

Edit: looking at $request->getRawHeaders(), I see this line in the headers:

cookies: vec6nb1egvvui6op7qr7b0oqf6

That obviously isn't right. The documentation for my version of Guzzle gives this example:

// Enable cookies and send specific cookies
$client->get('/get', ['cookies' => ['foo' => 'bar']]);

which looks to me to be consistent with what I am passing to Guzzle.

Just to be clear, I am not trying to manage cookies in both directions over multiple requests, so there is no need to store any cookies. I have a cookie name and its value (from another source), and I just want to make sure that name and value gets sent to the destination for a single GET request. I'm not trying to "maintain a session", but in a way I am having a session passed to me from another part of the application (not Guzzle) and need to set my Guzzle request up to use it.

Upvotes: 4

Views: 10991

Answers (1)

Jason
Jason

Reputation: 4772

Well, this seems to work. Guzzle was not sending the cookie without being sure the domain it was sending it to was correct:

// Set up a cookie - name, value AND domain.
$cookie = new Guzzle\Plugin\Cookie\Cookie();
$cookie->setName('PHPSESSID');
$cookie->setValue($session_id_from_api);
$cookie->setDomain($domain_of_my_service_url);

// Set up a cookie jar and add the cookie to it.
$jar = new Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar();
$jar->add($cookie);

// Set up the cookie plugin, giving it the cookie jar.
$plugin = new Guzzle\Plugin\Cookie\CookiePlugin($jar);

// Register the plugin with the client.
$client->addSubscriber($plugin);

// Now do the request as normal.
$request = $client->get($url);
$response = $client->send($request);

// The returned page body.
echo $response->getBody(true);

Upvotes: 11

Related Questions