http.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace app\common\util;
  3. class http
  4. {
  5. protected $baseUrl = 'http://localhost';
  6. protected $client;
  7. protected $cookieJar;
  8. function __construct()
  9. {
  10. $this->cookieJar = new \GuzzleHttp\Cookie\CookieJar();
  11. $config = [
  12. 'base_uri' => "",
  13. 'timeout' => 10.0,
  14. 'cookies' => $this->cookieJar,
  15. 'cookies' => true,
  16. //'proxy' => 'http://192.168.16.16:8888',
  17. // 'allow_redirects' => false,
  18. ];
  19. $config['proxy'] = 'http://192.168.16.96:8888';
  20. $this->client = new \GuzzleHttp\Client($config);
  21. }
  22. function request($uri, $method, $param, $header = [])
  23. {
  24. $method = strtolower($method);
  25. $options = [];
  26. $options['cookies'] = $this->cookieJar;
  27. $options['headers'] = $header;
  28. $options['verify'] = false;
  29. if (is_array($param)) {
  30. $options['form_params'] = $param;
  31. } elseif (is_string($param)) {
  32. $options['body'] = $param;
  33. }
  34. $method = strtoupper($method);
  35. $r = $this->client->request($method, $uri, $options);
  36. return $r;
  37. }
  38. }