HttpResponse.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // require_once 'includes/WebStart.php';
  3. class HttpResponse {
  4. public $http_code;
  5. public $data;
  6. public $http_info = array ();
  7. public $url;
  8. public $headers;
  9. public $method;
  10. public $postfields;
  11. public function __construct($url, $headers, $method = 'GET', $postfields = null) {
  12. $this->url = $url;
  13. $this->headers = $headers;
  14. $this->method = $method;
  15. if (! empty ( $postfields )) {
  16. $this->postfields = $postfields;
  17. }
  18. }
  19. public function getData() {
  20. if (DEBUG) {
  21. Logger::debug ( "url:" . $this->url );
  22. Logger::debug ( "data:" . $this->data );
  23. }
  24. return $this->data;
  25. }
  26. public function toString() {
  27. $str = "url:" . ($this->url) . "<br/>";
  28. $str .= "data:" . ($this->data) . "<br/>";
  29. $str .= "http_code:" . $this->http_code . "<br/>";
  30. $str .= "method:" . $this->method . "<br/>";
  31. return $str;
  32. }
  33. /**
  34. * 获取返回数据转化成Object
  35. *
  36. * @param boolean $throw
  37. * 是否抛出异常
  38. * @throws Exception
  39. * @return unknown
  40. */
  41. public function getDataAsObject($throw = false) {
  42. if (DEBUG) {
  43. Logger::debug ( "url:" . $this->url );
  44. Logger::debug ( "data:" . $this->data );
  45. }
  46. $obj = json_decode ( $this->data );
  47. if ($throw) {
  48. if ($obj->status->status_code != 0) {
  49. throw new Exception ( '请求url:' . $this->url . ' errCode:' . $data->status->status_code . ' errMsg:' . $data->status->status_reason );
  50. }
  51. }
  52. return $obj;
  53. }
  54. }
  55. ?>