我正在为一个网站创建一个API,允许用户通过API登录。我正在使用Guzzle,但问题是我如何在Guzzle中使用Cookies插件?在cURL中,我可以使用cookie文件,并将其与请求一起传递。但是这个例子看起来有点令人困惑。
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个请求?
发布于 2013-07-08 00:42:01
这只是个例子。你不需要提出三个请求。只需将cookie插件附加到您的客户端,就可以了。
https://stackoverflow.com/questions/17512370
复制相似问题