MultiRequestService multiGet($urls)


  • 用法
    基于GuzzleHttp的并发GET请求。

MultiRequestService对象方法列表:

  • concurrency():设置并发数
  • withOptions():设置GuzzleHttp的一些其他选项
  • withHeaders(): 设置HTTP Header
  • success(): HTTP success回调函数
  • error(): HTTP error回调函数
  • send(): 发送请求

用法

简单用法,默认并发数为5

  1. use GuzzleHttp\Psr7\Response;
  2. use QL\QueryList;
  3. $urls = [
  4. 'https://github.com/trending/go?since=daily',
  5. 'https://github.com/trending/html?since=daily',
  6. 'https://github.com/trending/java?since=daily'
  7. ];
  8. QueryList::multiGet($urls)
  9. ->success(function(QueryList $ql,Response $response, $index) use($urls){
  10. echo 'Current url: '.$urls[$index]."\r\n";
  11. $data = $ql->find('h3>a')->texts();
  12. print_r($data->all());
  13. })->send();

更高级的用法

  1. use GuzzleHttp\Psr7\Response;
  2. use QL\QueryList;
  3. $urls = [
  4. 'https://github.com/trending/go?since=daily',
  5. 'https://github.com/trending/html?since=daily',
  6. 'https://github.com/trending/java?since=daily'
  7. ];
  8. $rules = [
  9. 'name' => ['h3>a','text'],
  10. 'desc' => ['.py-1','text']
  11. ];
  12. $range = '.repo-list>li';
  13. QueryList::rules($rules)
  14. ->range($range)
  15. ->multiGet($urls)
  16. // 设置并发数为2
  17. ->concurrency(2)
  18. // 设置GuzzleHttp的一些其他选项
  19. ->withOptions([
  20. 'timeout' => 60
  21. ])
  22. // 设置HTTP Header
  23. ->withHeaders([
  24. 'User-Agent' => 'QueryList'
  25. ])
  26. // HTTP success回调函数
  27. ->success(function (QueryList $ql, Response $response, $index){
  28. $data = $ql->queryData();
  29. print_r($data);
  30. })
  31. // HTTP error回调函数
  32. ->error(function (QueryList $ql, $reason, $index){
  33. // ...
  34. })
  35. ->send();

为每个URL设置不同请求参数

  1. use GuzzleHttp\Psr7\Response;
  2. use QL\QueryList;
  3. $requests = [
  4. new Request('GET','http://httpbin.org/post',[
  5. 'User-Agent' => 'QueryList'
  6. ]),
  7. new Request('GET','http://httpbin.org/post',[
  8. 'User-Agent' => 'QueryList/3.0'
  9. ]),
  10. new Request('GET','http://httpbin.org/post',[
  11. 'User-Agent' => 'QueryList/4.0'
  12. ])
  13. ];
  14. QueryList::multiGet($requests)
  15. ->success(...)
  16. ->send();