使用Goutte下载图像

时间:2014-06-08 07:44:02

标签: php cookies curl web-scraping goutte

我必须在请求中使用cookie下载图像。我可以使用file_get_contents(使用stream_context_create)或curl传递cookie。但是如何用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'));

1 个答案:

答案 0 :(得分:2)

使用Goutte 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”后使用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();