无法使用Guzzle请求发送Cookie [英] Cannot sent cookie with Guzzle request

查看:3322
本文介绍了无法使用Guzzle请求发送Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,需要从应用程序获取CSV文件。有一个应用程序的API允许脚本批量,这给脚本一个会话cookie进行身份验证。



使用curl目录工作原理:

  $ 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;

使用从API登录获取的会话ID获取CSV文件,并通过coookie传递。 / p>

现在,我想使用Guzzle做同样的事情,但我只是得到登录页面。这是我使用的代码:

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

这给我登录页面,所以GET到应用程序不能识别由



我还需要做什么来确保我指定的cookie和值被发送到远程应用程序?



编辑:查看 $ request-> getRawHeaders(),我在标题中看到这一行:

  cookies:vec6nb1egvvui6op7qr7b0oqf6 

对。我的版本的Guzzle的文档给出了这个例子:

  //启用cookies并发送特定的cookie 
$ client- > get('/ get',['cookies'=> ['foo'=>'bar']]);

这看起来与我传给Guzzle的一致。



为了清楚起见,我没有尝试通过多个请求在两个方向管理Cookie,因此不需要存储任何Cookie。我有一个cookie名称及其值(从另一个来源),我只是想确保名称和值发送到目的地的单个GET请求。我不是想保持会话,但在某种程度上,我有一个会话从应用程序的另一部分(不是Guzzle)传递给我,需要设置我的Guzzle请求使用它。

解决方案

好吧,这似乎工作。 Guzzle没有发送cookie,而不确保它发送它的域是正确的:

  //设置一个cookie  - 名称,值AND域。 
$ cookie = new Guzzle\Plugin\Cookie\ Cookie();
$ cookie-> setName('PHPSESSID');
$ cookie-> setValue($ session_id_from_api);
$ cookie-> setDomain($ domain_of_my_service_url);

//设置一个cookie jar并添加cookie到它。
$ jar = new Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar();
$ jar-> add($ cookie);

//设置cookie插件,给它的cookie jar。
$ plugin = new Guzzle\Plugin\Cookie\CookiePlugin($ jar);

//向客户端注册插件。
$ client-> addSubscriber($ plugin);

//现在正常执行请求。
$ request = $ client-> get($ url);
$ response = $ client-> send($ request);

//返回的页面体。
echo $ response-> getBody(true);


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.

解决方案

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);

这篇关于无法使用Guzzle请求发送Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆