http.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. $http = new \Qii_Library_Http();
  2. // set cookie file
  3. $http->setCookiePath('cookie.dat');
  4. // add text/plain header for web sapi handler
  5. if (php_sapi_name() !== 'cli') {
  6. header('Content-Type: text/plain');
  7. }
  8. // simple load response contents
  9. echo '1. loading content of baidu ... ';
  10. $response = $http->get('http://www.baidu.com');
  11. echo number_format(strlen($response)) . ' bytes', PHP_EOL;
  12. echo '2. fetching search results of `php\' in baidu ... ';
  13. $response = $http->get('http://www.baidu.com/s', ['wd' => 'php']);
  14. echo number_format(strlen($response)) . ' bytes', PHP_EOL;
  15. echo ' time costs: ', $response->timeCost, PHP_EOL;
  16. echo ' response.status: ', $response->status, ' ', $response->statusText, PHP_EOL;
  17. echo ' response.headers.content-type: ', $response->getHeader('content-type'), PHP_EOL;
  18. echo ' response.headers.transfer-encoding: ', $response->getHeader('transfer-encoding'), PHP_EOL;
  19. echo ' response.cookies.BDSVRTM: ', $response->getCookie('BDSVRTM'), PHP_EOL;
  20. echo '3. testing error response ... ';
  21. $response = $http->get('http://127.0.0.1:65535');
  22. echo $response->hasError() ? 'ERROR: ' . $response->error : 'OK', PHP_EOL;
  23. echo '4. test head request to baidu ... ';
  24. $response = $http->head('http://www.baidu.com');
  25. echo number_format(strlen($response)) . ' bytes', PHP_EOL;
  26. echo ' response.headers.server: ', $response->getHeader('server'), PHP_EOL;
  27. echo '5. post request to baidu ... ';
  28. //Client::debug('open');
  29. $response = $http->post('http://www.baidu.com/s', ['wd' => 'php', 'ie' => 'utf-8']);
  30. echo number_format(strlen($response)) . ' bytes', PHP_EOL;
  31. if ($response->hasError()) {
  32. echo ' response.error: ', $response->error, PHP_EOL;
  33. }
  34. echo '6. testing postJSON request ...';
  35. $data = $http->postJson('http://api.mcloudlife.com/api/version');
  36. echo 'OK', PHP_EOL;
  37. echo ' response.json: ', json_encode($data), PHP_EOL;
  38. echo '7. customize request for restful API ... ';
  39. $request = new Request('http://api.mcloudlife.com/open/record/bp/1024');
  40. $request->setMethod('GET');
  41. $request->setHeader('user-agent', 'mCloud/2.4.3D');
  42. // bearer token authorization
  43. $request->setHeader('authorization', 'Bearer f4fe27fe5f270a4e8edc1a07289452d1');
  44. $request->setHeader('accept', 'application/json');
  45. $response = $http->exec($request);
  46. if ($response->hasError()) {
  47. echo 'ERROR', PHP_EOL;
  48. echo ' response.error: ', $response->error, PHP_EOL;
  49. } else {
  50. echo 'OK', PHP_EOL;
  51. echo ' response.status: ', $response->status, ' ', $response->statusText, PHP_EOL;
  52. echo ' response.body: ', $response->body, PHP_EOL;
  53. }
  54. echo '8. post request & upload files ... ';
  55. $request = new Request('http://hightman.cn/post.php');
  56. $request->setMethod('POST');
  57. $request->setHeader('x-server-ip', '202.75.216.234');
  58. $request->addPostField('post1', 'post1-value');
  59. $request->addPostField('post2', 'post2-value');
  60. $request->addPostFile('upload1', 'upload1-name.txt', 'hi, just a test');
  61. $request->addPostFile('upload2', __FILE__);
  62. $response = $http->exec($request);
  63. echo number_format(strlen($response)) . ' bytes', PHP_EOL;
  64. echo $response, PHP_EOL;
  65. echo '9. multiple get requests in parallel ... ', PHP_EOL;
  66. // define callback as normal function
  67. function test_cb($res, $req, $key)
  68. {
  69. echo ' ', $req->getUrl(), ', ', number_format(strlen($res)), ' bytes in ', sprintf('%.4f', $res->timeCost), 's', PHP_EOL;
  70. // even you can redirect HERE
  71. if ($key === 'baidu' && !strstr($req->getUrl(), 'czxiu')) {
  72. $res->redirect('http://www.czxiu.com');
  73. }
  74. }
  75. $http->setParser('test_cb');
  76. $responses = $http->mget([
  77. 'baidu' => 'http://www.baidu.com',
  78. 'sina' => 'http://news.sina.com.cn',
  79. 'qq' => 'http://www.qq.com',
  80. ]);
  81. echo '10. process multiple various requests in parallel ... ', PHP_EOL;
  82. // define callback as object
  83. class testCb implements \hightman\http\ParseInterface
  84. {
  85. public function parse(Response $res, Request $req, $key)
  86. {
  87. echo ' ', $req->getMethod(), ' /', $key, ' finished, ', number_format(strlen($res)), ' bytes in ', sprintf('%.4f', $res->timeCost), 's', PHP_EOL;
  88. }
  89. }
  90. // construct requests
  91. $requests = [];
  92. $requests['version'] = new Request('http://api.mcloudlife.com/api/version', 'POST');
  93. $requests['baidu'] = new Request('http://www.baidu.com/s?wd=php');
  94. $request = new Request('http://api.mcloudlife.com/open/auth/token');
  95. $request->setMethod('POST');
  96. $request->setHeader('accept', 'application/json');
  97. $request->setJsonBody([
  98. 'client_id' => 'client_id',
  99. 'client_secret' => 'client_secret',
  100. ]);
  101. $requests['token'] = $request;
  102. $http->setParser(new testCb());
  103. $responses = $http->exec($requests);