【发布时间】:2013-07-07 13:01:01
【问题描述】:
我正在为允许用户通过 API 登录的网站创建 API。我正在使用 Guzzle,但问题是如何将 Cookies 插件与 Guzzle 一起使用?在 cURL 中,我可以使用 cookie 文件,并将其与请求一起传递。但是 Guzzle 文档上的示例看起来令人困惑。
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
$cookiePlugin = new CookiePlugin(new ArrayCookieJar());
// Add the cookie plugin to a client
$client = new Client('http://www.test.com/');
$client->addSubscriber($cookiePlugin);
// Send the request with no cookies and parse the returned cookies
$client->get('http://www.yahoo.com/')->send();
// Send the request again, noticing that cookies are being sent
$request = $client->get('http://www.yahoo.com/');
$request->send();
echo $request;
它似乎发出了 3 个请求。我不明白为什么它向 test.com 发出请求,然后向 yahoo.com 发出两次请求。为什么您无法提出 1 个请求?
【问题讨论】:
-
为什么不删除其他代码行并坚持 1 个请求
-
以上代码来自文档。我不明白为什么它会发出 3 个请求。我和你在一起,为什么不只是提出1个请求。
-
也许是为了向你展示不同的请求方式