1

我必须在请求中使用 cookie 下载图像。我可以使用 file_get_contents(使用 stream_context_create)或使用传递 cookie 的 curl 来实现。但是如何使用 Goutte 来实现呢?

use Goutte\Client;

$client = new Client();
$response = $client->request('GET', 'https://www.google.pl/images/srpr/logo11w.png');

我发出 GET 请求,下一步是什么?

好的,我想通了:

$client->get('https://www.google.pl/images/srpr/logo11w.png', array('save_to' => __DIR__.'/image.jpg'));
4

1 回答 1

2

使用痛风 2:

$client = new GuzzleHttp\Client([
    'base_url' => 'https://www.google.pl',
    'defaults' => [
        'cookies' => true,
    ]
]);

$response = $client->post('/login', [
    'body' => [
        'login'    => $login,
        'password' => $password
    ]
]);

$response = $client->get('/images/srpr/logo11w.png');

$image = $response->getBody();

在将 "guzzle/plugin-cookie": "~3.1" 添加到 Composer 后使用 Goutte 1:

use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;

$client = new Client('https://www.google.pl');

$client->addSubscriber(new CookiePlugin(new ArrayCookieJar()));

$response = $client->post('/login', '', array('login' => $login, 'password' => $password))->send();

$response = $client->get('/images/srpr/logo11w.png')->send();

$image = $response->getBody();
于 2014-07-23T20:18:29.433 回答