对我来说,使用 GuzzleClient 不起作用。我使用了 getCookieJar 返回的 CookieJar。我在最初的问题中看到的唯一错误是您尝试仅通过提供字符串值来设置 cookie。set 方法需要一个 Cookie 实例才能工作。方法签名是:
/**
* Sets a cookie.
*
* @param Cookie $cookie A Cookie instance
*/
public function set(Cookie $cookie)
例子:
$this->client->getCookieJar()->set(new Cookie($name, $value, null, null, $domain));
注意不要对 cookie 值进行编码或将 encodedValue 设置为 true
Cookie __construct 的签名:
/**
* Sets a cookie.
*
* @param string $name The cookie name
* @param string $value The value of the cookie
* @param string $expires The time the cookie expires
* @param string $path The path on the server in which the cookie will be available on
* @param string $domain The domain that the cookie is available
* @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
* @param bool $httponly The cookie httponly flag
* @param bool $encodedValue Whether the value is encoded or not
*/
public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)