2015-04-05 161 views
0

這是代碼我使用發送多個GOUTTE請求異步

require_once 'goutte.phar'; 
use Goutte\Client; 
$client = new Client(); 
for($i=0;$i<10;$i++){ 
    $crawler = $client->request('GET', 'http://website.com'); 
    echo '<p>'.$crawler->filterXpath('//meta[@property="og:description"]')->attr('content').'</p>'; 
    echo '<p>'.$crawler->filter('title')->text().'</p>'; 
} 

這工作,但需要花費大量的時間來處理?有沒有辦法更快地做到這一點。

回答

1

對於初學者來說,代碼示例沒有任何異步。這意味着您的應用程序將按順序執行get請求,等待響應,解析響應並返回。

雖然Goutte在內部使用Guzzle,但它並未使用Guzzles異步功能。

要真正使你的代碼異步你將要參考狂飲文檔上:

你上面的代碼示例會導致類似:

require 'vendor/autoload.php' //assuming composer package management. 

$client = new GuzzleHttp\Client(); 

$requests = [ 
    $client->createRequest('GET', $url1), 
    $client->createRequest('GET', $url2), 
    $client->createRequest('GET', $url3), 
    $client->createRequest('GET', $url4), 
    $client->createRequest('GET', $url5), 
    $client->createRequest('GET', $url6), 
    $client->createRequest('GET', $url7), 
    $client->createRequest('GET', $url8), 
    $client->createRequest('GET', $url9), 
    $client->createRequest('GET', $url10), 
]; 

$options = [ 
    'complete' => [ 
     [ 
      'fn' => function (CompleteEvent $event) { 
       $crawler = new Symfony\Component\DomCrawler\Crawler(null, $event->getRequest()->getUrl()); 
       $crawler->addContent($event->getResponse->getBody(), $event->getResponse()->getHeader('Content-Type')); 
       echo '<p>'.$crawler->filterXpath('//meta[@property="og:description"]')->attr('content').'</p>'; 
       echo '<p>'.$crawler->filter('title')->text().'</p>'; 
      }, 
      'priority' => 0, // Optional 
      'once'  => false // Optional 
     ] 
    ] 
]; 

$pool = new GuzzleHttp\Pool($client, $requests, $options); 

$pool->wait(); 
+0

嘿肖恩,謝謝你的解釋。儘管這段代碼中出現'Uncaught exception'InvalidArgumentException'消息'錯誤。 – 2015-04-05 16:35:59

+0

@PriyaRawat - 當您提供的信息與所提供的信息一樣少時,要求我排除類似於描述的錯誤時,不公平或合理。話雖如此,我敢冒險猜測您正在嘗試使用Goutte附帶的Guzzle版本。根據您使用的Goutte版本(可能)將不會有正確版本的Guzzle。確保composer.json文件列出guzzlehttp \ guzzle和symfony \ dom-crawler。 – 2015-04-06 02:59:42