Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • ApplicationProxy
  • CrudProxy
  • DomainProxy
  • HttpRequest
  • QueryString
  • ReportingProxy
  • RestHttp
  • StandardProxy
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace NGS\Client;
  3: 
  4: /**
  5:  * Request object used by {@see NGS\Client\RestHttp}
  6:  */
  7: class HttpRequest
  8: {
  9:     private $uri;
 10:     private $curl;
 11:     private $method;
 12:     private $options;
 13:     private $responseInfo;
 14:     private $responseHeaders;
 15: 
 16:     public function __construct($uri, $method = null, $body = null, $headers = null, $options = null)
 17:     {
 18:         $this->uri = $uri;
 19:         $this->curl = curl_init($uri);
 20: 
 21:         $this->options = array(
 22:             CURLOPT_RETURNTRANSFER  => true,
 23:             CURLINFO_HEADER_OUT     => true,
 24:         );
 25: 
 26:         if (is_array($options)) {
 27:             $this->options += $options;
 28:         }
 29: 
 30:         $this->method = $method;
 31: 
 32:         if($method !== null)
 33:             $this->method($method);
 34:         if($body !== null)
 35:             $this->body($body);
 36:         if($headers !== null)
 37:             $this->headers($headers);
 38:     }
 39: 
 40:     public function headers($headers)
 41:     {
 42:         if(!isset($this->options[CURLOPT_HTTPHEADER]))
 43:             $this->options[CURLOPT_HTTPHEADER] = array();
 44: 
 45:         if(is_array($headers)) {
 46:             foreach($headers as $key => $value)
 47:                 $this->options[CURLOPT_HTTPHEADER][] = $value;
 48:         }
 49:         else if(is_string($headers)) {
 50:             $this->options[CURLOPT_HTTPHEADER][] = $headers;
 51:         }
 52:         return $this;
 53:     }
 54: 
 55:     public function method($method)
 56:     {
 57:         $method = strtoupper($method);
 58:         if ($method === 'POST') {
 59:             $this->options[CURLOPT_POST] = true;
 60:         } else {
 61:             $this->options[CURLOPT_CUSTOMREQUEST] = $method;
 62:         }
 63:         return $this;
 64:     }
 65: 
 66:     public function body($body)
 67:     {
 68:         $this->options[CURLOPT_POSTFIELDS] = $body;
 69:     }
 70: 
 71:     public function send()
 72:     {
 73:         curl_setopt_array($this->curl, $this->options);
 74: 
 75:         $response = curl_exec($this->curl);
 76:         $this->responseInfo = curl_getinfo($this->curl);
 77: 
 78:         return $response;
 79:     }
 80: 
 81:     public function getResponseInfo()
 82:     {
 83:         return $this->responseInfo;
 84:     }
 85: 
 86:     public function getResponseHeaders()
 87:     {
 88:         return $this->getResponseInfo();
 89:     }
 90: 
 91:     public function getResponseCode()
 92:     {
 93:         return isset($this->responseInfo['http_code']) ? $this->responseInfo['http_code'] : null;
 94:     }
 95: 
 96:     public function getResponseContentType()
 97:     {
 98:         return isset($this->responseInfo['content_type']) ? $this->responseInfo['content_type'] : null;
 99:     }
100: 
101:     public function getError()
102:     {
103:         $error = curl_error($this->curl);
104:         return $error ? $error : null;
105:     }
106: 
107:     public function __toString()
108:     {
109:         $headers = isset($this->options[CURLOPT_HTTPHEADER]) ? $this->options[CURLOPT_HTTPHEADER] : array();
110:         $body = isset($this->options[CURLOPT_POSTFIELDS]) ? $this->options[CURLOPT_POSTFIELDS] : '';
111:         return strtoupper($this->method).' '.$this->uri."\n"
112:             .implode("\n", $headers)."\n"
113:             .$body;
114:     }
115: }
116: 
API documentation generated by ApiGen 2.8.0