Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在痛风中设置饼干?_Php_Cookies_Setcookie_Goutte - Fatal编程技术网

Php 如何在痛风中设置饼干?

Php 如何在痛风中设置饼干?,php,cookies,setcookie,goutte,Php,Cookies,Setcookie,Goutte,我不知道如何在Goutte中设置cookies。我正在尝试以下代码: $client->setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36'); $client->getCookieJar()->set('SRCHUID'); 我附加了一

我不知道如何在Goutte中设置cookies。我正在尝试以下代码:

$client->setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36');
$client->getCookieJar()->set('SRCHUID');
我附加了一个具有此名称的cookie图像。我如何设置这个饼干

痛风带口吻6

use GuzzleHttp\Cookie;

$cookieJar = new \GuzzleHttp\Cookie\CookieJar(true);

$cookieJar->setCookie(new \GuzzleHttp\Cookie\SetCookie([
       'Domain'  => "www.domain.com",
       'Name'    => $name,
       'Value'   => $value,
       'Discard' => true
 ]));

 $client = new Client();
 $guzzleclient = new \GuzzleHttp\Client([
        'timeout' => 900,
        'verify' => false,
        'cookies' => $cookieJar
  ]);
  $client->setClient($guzzleclient);

  return $client; //or do your normal client request here e.g $client->request('GET', $url);

对我来说,使用Guzzle客户端不起作用。我使用了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\uuu构造的签名:

/**
 * 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)

如果您找到了答案,请在此处发布这对我有很大帮助: