Http.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace WY\app\libs;
  3. if (!defined('WY_ROOT')) {
  4. exit;
  5. }
  6. class Http
  7. {
  8. private $resCode;
  9. private $errInfo;
  10. private $resContent;
  11. function __construct($url, $data, $build = 0, $timeout = 15)
  12. {
  13. $this->url = $url;
  14. $this->data = $data;
  15. $this->timeout = $timeout;
  16. $this->build = $build;
  17. }
  18. public function toUrl()
  19. {
  20. $ch = curl_init();
  21. curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  22. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  25. curl_setopt($ch, CURLOPT_POST, 1);
  26. curl_setopt($ch, CURLOPT_URL, $this->url);
  27. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->build ? http_build_query($this->data) : $this->data);
  28. $res = curl_exec($ch);
  29. $this->resCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  30. if ($res == NULL) {
  31. $this->errInfo = "call http err :" . curl_errno($ch) . " - " . curl_error($ch);
  32. curl_close($ch);
  33. return false;
  34. } else {
  35. if ($this->resCode != "200") {
  36. $this->errInfo = "call http err httpcode=" . $this->resCode;
  37. curl_close($ch);
  38. return false;
  39. }
  40. }
  41. curl_close($ch);
  42. $this->resContent = $res;
  43. return true;
  44. }
  45. public function getResContent()
  46. {
  47. return $this->resContent;
  48. }
  49. public function getResCode()
  50. {
  51. return $this->resCode;
  52. }
  53. public function getErrInfo()
  54. {
  55. return $this->errInfo;
  56. }
  57. }
  58. ?>