Permalink
Browse files

Updating JHttp and transports to support sending data from either an

array or a string.  This should allow more flexibility for various
service requests.
  • Loading branch information...
1 parent 903e1e9 commit 649e5ccb4ef95c3c770ec2388941561a84394b2d @LouisLandry LouisLandry committed with Nov 6, 2011
View
8 libraries/joomla/http/http.php
@@ -91,14 +91,14 @@ public function get($url, array $headers = null)
* Method to send the POST command to the server.
*
* @param string $url Path to the resource.
- * @param array $data Associative array of key/value pairs to send as values.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of name-value pairs to include in the header of the request.
*
* @return JHttpResponse
*
* @since 11.4
*/
- public function post($url, array $data, array $headers = null)
+ public function post($url, $data, array $headers = null)
{
return $this->transport->request('POST', new JUri($url), $data, $headers);
}
@@ -107,14 +107,14 @@ public function post($url, array $data, array $headers = null)
* Method to send the PUT command to the server.
*
* @param string $url Path to the resource.
- * @param array $data Associative array of key/value pairs to send as values.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of name-value pairs to include in the header of the request.
*
* @return JHttpResponse
*
* @since 11.4
*/
- public function put($url, array $data, array $headers = null)
+ public function put($url, $data, array $headers = null)
{
return $this->transport->request('PUT', new JUri($url), $data, $headers);
}
View
4 libraries/joomla/http/interface.php
@@ -23,7 +23,7 @@
*
* @param string $method The HTTP method for sending the request.
* @param JUri $uri The URI to the resource to request.
- * @param array $data An array of key => value pairs to send with the request.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
@@ -32,5 +32,5 @@
*
* @since 11.4
*/
- public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null);
+ public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null);
}
View
16 libraries/joomla/http/transports/curl.php
@@ -39,7 +39,7 @@ public function __construct()
*
* @param string $method The HTTP method for sending the request.
* @param JUri $uri The URI to the resource to request.
- * @param array $data An array of key => value pairs to send with the request.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
@@ -48,7 +48,7 @@ public function __construct()
*
* @since 11.4
*/
- public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null)
+ public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
// Setup the cURL handle.
$ch = curl_init();
@@ -59,8 +59,16 @@ public function request($method, JUri $uri, array $data = null, array $headers =
// If data exists let's encode it and make sure our Content-type header is set.
if (isset($data))
{
- // Add the encoded content into the stream context options array.
- $options[CURLOPT_POSTFIELDS] = http_build_query($data);
+ // If the data is a scalar value simply add it to the cURL post fields.
+ if (is_scalar($data))
+ {
+ $options[CURLOPT_POSTFIELDS] = $data;
+ }
+ // Otherwise we need to encode the value first.
+ else
+ {
+ $options[CURLOPT_POSTFIELDS] = http_build_query($data);
+ }
if (!isset($headers['Content-type']))
{
View
8 libraries/joomla/http/transports/socket.php
@@ -45,7 +45,7 @@ public function __construct()
*
* @param string $method The HTTP method for sending the request.
* @param JUri $uri The URI to the resource to request.
- * @param array $data An array of key => value pairs to send with the request.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
@@ -55,7 +55,7 @@ public function __construct()
* @since 11.4
* @throws RuntimeException
*/
- public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null)
+ public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
$connection = $this->connect($uri, $timeout);
@@ -80,8 +80,8 @@ public function request($method, JUri $uri, array $data = null, array $headers =
// If we have data to send make sure our request is setup for it.
if (!empty($data))
{
- // If the data is an array, build the request query string.
- if (is_array($data))
+ // If the data is not a scalar value encode it to be sent with the request.
+ if (!is_scalar($data))
{
$data = http_build_query($data);
}
View
16 libraries/joomla/http/transports/stream.php
@@ -46,7 +46,7 @@ public function __construct()
*
* @param string $method The HTTP method for sending the request.
* @param JUri $uri The URI to the resource to request.
- * @param array $data An array of key => value pairs to send with the request.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
@@ -55,16 +55,24 @@ public function __construct()
*
* @since 11.4
*/
- public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null)
+ public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
// Create the stream context options array with the required method offset.
$options = array('method' => strtoupper($method));
// If data exists let's encode it and make sure our Content-type header is set.
if (isset($data))
{
- // Add the encoded content into the stream context options array.
- $options['content'] = http_build_query($data);
+ // If the data is a scalar value simply add it to the stream context options.
+ if (is_scalar($data))
+ {
+ $options['content'] = $data;
+ }
+ // Otherwise we need to encode the value first.
+ else
+ {
+ $options['content'] = http_build_query($data);
+ }
if (!isset($headers['Content-type']))
{

0 comments on commit 649e5cc

Please sign in to comment.