如何使用PHP Goutte发送Cookie


How to send Cookie using PHP Goutte

我在PHP Goutte 中有这段代码

$client = new GuzzleHttp'Client([
        'base_uri' => 'http://www.yellowpages.com.au',
        'cookies' => true,
        'headers' =>  [
            'Accept'          => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'zip, deflate, sdch', 
            'Accept-Language' => 'en-US,en;q=0.8', 
            'Cache-Control'   => 'max-age=0',
            'User-Agent'      => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0'
        ]
    ]);

我想将我的自定义Cookie发送到服务器

通常我使用简单的cURL 这样做

$a_curl_opts = array(
    CURLOPT_NOBODY => 0,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_COOKIEFILE => $file_cook,
    CURLOPT_COOKIEJAR => $file_cook,
);
curl_setopt_array($curl_init, $a_curl_opts);

我在Goutte 的某个地方看到了这件事

$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_COOKIEFILE, $file_cook); 
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_COOKIEJAR, $file_cook); 

但我犯了这个错误。

Catchable fatal error: Argument 3 passed to GuzzleHttp'Client::request() must be of the type array, string given, called in D:'Ampps'www'gum'vendor'guzzlehttp'guzzle'src'Client.php on line 87 and defined in D:'Ampps'www'gum'vendor'guzzlehttp'guzzle'src'Client.php on line 126

试试这个:

 $client = new 'GuzzleHttp'Client(array(
             'curl' => array(
                 CURLOPT_COOKIEFILE => $cookie_file,
                 CURLOPT_COOKIEJAR => $cookie_file,
                 CURLOPT_RETURNTRANSFER  => 1
             ),
             'cookies' => true
         ));