77 */
88class Connection
99{
10+ /**
11+ * XML media type.
12+ */
13+ const MEDIA_TYPE_XML = 'application/xml ' ;
14+ /**
15+ * JSON media type.
16+ */
17+ const MEDIA_TYPE_JSON = 'application/json ' ;
18+ /**
19+ * Default urlencoded media type.
20+ */
21+ const MEDIA_TYPE_WWW = 'application/x-www-form-urlencoded ' ;
1022
1123 /**
1224 * @var resource cURL resource
@@ -60,15 +72,19 @@ class Connection
6072
6173 /**
6274 * Deal with failed requests if failOnError is not set.
63- * @var string | false
75+ * @var string| false
6476 */
6577 private $ lastError = false ;
6678
6779 /**
68- * Determines whether requests and responses should be treated
69- * as XML. Defaults to false (using JSON).
80+ * Determines whether the response body should be returned as a raw string.
7081 */
71- private $ useXml = false ;
82+ private $ rawResponse = false ;
83+
84+ /**
85+ * Determines the default content type to use with requests and responses.
86+ */
87+ private $ contentType ;
7288
7389 /**
7490 * Initializes the connection object.
@@ -96,7 +112,23 @@ public function __construct()
96112 */
97113 public function useXml ($ option = true )
98114 {
99- $ this ->useXml = $ option ;
115+ if ($ option ) {
116+ $ this ->contentType = self ::MEDIA_TYPE_XML ;
117+ $ this ->rawResponse = true ;
118+ }
119+ }
120+
121+ /**
122+ * Controls whether requests or responses should be treated
123+ * as urlencoded form data.
124+ *
125+ * @param bool $option the new state of this feature
126+ */
127+ public function useUrlEncoded ($ option = true )
128+ {
129+ if ($ option ) {
130+ $ this ->contentType = self ::MEDIA_TYPE_WWW ;
131+ }
100132 }
101133
102134 /**
@@ -125,11 +157,23 @@ public function failOnError($option = true)
125157 * @param string $username
126158 * @param string $password
127159 */
128- public function authenticate ($ username , $ password )
160+ public function authenticateBasic ($ username , $ password )
129161 {
130162 curl_setopt ($ this ->curl , CURLOPT_USERPWD , "$ username: $ password " );
131163 }
132164
165+ /**
166+ * Sets Oauth authentication headers
167+ *
168+ * @param string $clientId
169+ * @param string $authToken
170+ */
171+ public function authenticateOauth ($ clientId , $ authToken )
172+ {
173+ $ this ->addHeader ('X-Auth-Client ' , $ clientId );
174+ $ this ->addHeader ('X-Auth-Token ' , $ authToken );
175+ }
176+
133177 /**
134178 * Set a default timeout for the request. The client will error if the
135179 * request takes longer than this to respond.
@@ -168,6 +212,9 @@ public function verifyPeer($option = false)
168212
169213 /**
170214 * Add a custom header to the request.
215+ *
216+ * @param string $header
217+ * @param string $value
171218 */
172219 public function addHeader ($ header , $ value )
173220 {
@@ -176,6 +223,7 @@ public function addHeader($header, $value)
176223
177224 /**
178225 * Remove a header from the request.
226+ *
179227 * @param string $header
180228 */
181229 public function removeHeader ($ header )
@@ -185,10 +233,12 @@ public function removeHeader($header)
185233
186234 /**
187235 * Get the MIME type that should be used for this request.
236+ *
237+ * Defaults to application/json
188238 */
189239 private function getContentType ()
190240 {
191- return ($ this ->useXml ) ? ' application/xml ' : ' application/json ' ;
241+ return ($ this ->contentType ) ? $ this -> contentType : self :: MEDIA_TYPE_JSON ;
192242 }
193243
194244 /**
@@ -197,7 +247,6 @@ private function getContentType()
197247 */
198248 private function initializeRequest ()
199249 {
200- $ this ->isComplete = false ;
201250 $ this ->responseBody = '' ;
202251 $ this ->responseHeaders = array ();
203252 $ this ->lastError = false ;
@@ -222,7 +271,7 @@ private function handleResponse()
222271 throw new NetworkError (curl_error ($ this ->curl ), curl_errno ($ this ->curl ));
223272 }
224273
225- $ body = ($ this ->useXml ) ? $ this ->getBody () : json_decode ($ this ->getBody ());
274+ $ body = ($ this ->rawResponse ) ? $ this ->getBody () : json_decode ($ this ->getBody ());
226275
227276 $ status = $ this ->getStatus ();
228277
@@ -481,6 +530,8 @@ public function getBody()
481530 /**
482531 * Access given header from the response.
483532 *
533+ * @param string $header Header name to retrieve
534+ *
484535 * @return string|void
485536 */
486537 public function getHeader ($ header )
0 commit comments