【问题标题】:Images download using Goutte使用 Goutte 下载图像
【发布时间】:2014-07-29 00:49:00
【问题描述】:

我必须在请求中使用 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'));

【问题讨论】:

    标签: php cookies curl web-scraping goutte


    【解决方案1】:

    使用 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" 添加到 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();
    

    【讨论】:

      猜你喜欢
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2012-08-18
      • 2013-06-23
      • 2015-11-07
      • 2017-11-27
      相关资源
      最近更新 更多