Skip to content

Commit

Permalink
Updating JHttp and transports to support sending data from either an
Browse files Browse the repository at this point in the history
array or a string.  This should allow more flexibility for various
service requests.
  • Loading branch information
LouisLandry authored and Ian MacLennan committed Nov 13, 2011
1 parent 903e1e9 commit 649e5cc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
8 changes: 4 additions & 4 deletions libraries/joomla/http/http.php
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/joomla/http/interface.php
Expand Up @@ -23,7 +23,7 @@ interface JHttpTransport
*
* @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.
Expand All @@ -32,5 +32,5 @@ interface JHttpTransport
*
* @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);
}
16 changes: 12 additions & 4 deletions libraries/joomla/http/transports/curl.php
Expand Up @@ -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.
Expand All @@ -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();
Expand All @@ -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']))
{
Expand Down
8 changes: 4 additions & 4 deletions libraries/joomla/http/transports/socket.php
Expand Up @@ -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.
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
16 changes: 12 additions & 4 deletions libraries/joomla/http/transports/stream.php
Expand Up @@ -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.
Expand All @@ -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']))
{
Expand Down

0 comments on commit 649e5cc

Please sign in to comment.