EasySwoole 协程HTTPClient组件

协程httpClient组件,基于swoole 异步http client客户端实现,可在协程内发起http请求不被阻塞,可用于下载文件,请求api,爬虫等一系列需求当中

安装

  1. composer require easyswoole/http-client

单次请求:

  1. <?php
  2. $url = 'http://docker.local.com/test.php/?get1=get1';
  3. $test = new \EasySwoole\HttpClient\HttpClient($url);
  4. //$test->post();
  5. $test->addCookie('c1','c1')->addCookie('c2','c2');
  6. $test->post([
  7. 'post1'=>'post1'
  8. ]);
  9. $test->setHeader('myHeader','myHeader');
  10. $test->addData('sasasas','test.file','text','test.file');
  11. //$test->postJSON(json_encode(['json'=>1]));
  12. $ret = $test->exec();
  13. var_dump($ret->getBody());

并发请求:

  1. <?php
  2. $url = 'http://docker.local.com/test.php/?get1=get1';
  3. $test = new \EasySwoole\HttpClient\HttpClient($url);
  4. $multi = new \EasySwoole\HttpClient\Multi();
  5. $multi->addTask('t1',$test);
  6. $multi->addTask('t2',$test);
  7. $ret = $multi->exec();
  8. foreach ($ret as $taskName => $response){
  9. var_dump("task {$taskName} finish and body is {$response->getBody()}");
  10. }