Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 649e5ccb4ef95c3c770ec2388941561a84394b2d 1 parent 903e1e9
Louis Landry authored November 05, 2011 ianmacl committed November 13, 2011
8  libraries/joomla/http/http.php
@@ -91,14 +91,14 @@ public function get($url, array $headers = null)
91 91
 	 * Method to send the POST command to the server.
92 92
 	 *
93 93
 	 * @param   string  $url      Path to the resource.
94  
-	 * @param   array   $data     Associative array of key/value pairs to send as values.
  94
+	 * @param   mixed   $data     Either an associative array or a string to be sent with the request.
95 95
 	 * @param   array   $headers  An array of name-value pairs to include in the header of the request.
96 96
 	 *
97 97
 	 * @return  JHttpResponse
98 98
 	 *
99 99
 	 * @since   11.4
100 100
 	 */
101  
-	public function post($url, array $data, array $headers = null)
  101
+	public function post($url, $data, array $headers = null)
102 102
 	{
103 103
 		return $this->transport->request('POST', new JUri($url), $data, $headers);
104 104
 	}
@@ -107,14 +107,14 @@ public function post($url, array $data, array $headers = null)
107 107
 	 * Method to send the PUT command to the server.
108 108
 	 *
109 109
 	 * @param   string  $url      Path to the resource.
110  
-	 * @param   array   $data     Associative array of key/value pairs to send as values.
  110
+	 * @param   mixed   $data     Either an associative array or a string to be sent with the request.
111 111
 	 * @param   array   $headers  An array of name-value pairs to include in the header of the request.
112 112
 	 *
113 113
 	 * @return  JHttpResponse
114 114
 	 *
115 115
 	 * @since   11.4
116 116
 	 */
117  
-	public function put($url, array $data, array $headers = null)
  117
+	public function put($url, $data, array $headers = null)
118 118
 	{
119 119
 		return $this->transport->request('PUT', new JUri($url), $data, $headers);
120 120
 	}
4  libraries/joomla/http/interface.php
@@ -23,7 +23,7 @@
23 23
 	 *
24 24
 	 * @param   string   $method     The HTTP method for sending the request.
25 25
 	 * @param   JUri     $uri        The URI to the resource to request.
26  
-	 * @param   array    $data       An array of key => value pairs to send with the request.
  26
+	 * @param   mixed    $data       Either an associative array or a string to be sent with the request.
27 27
 	 * @param   array    $headers    An array of request headers to send with the request.
28 28
 	 * @param   integer  $timeout    Read timeout in seconds.
29 29
 	 * @param   string   $userAgent  The optional user agent string to send with the request.
@@ -32,5 +32,5 @@
32 32
 	 *
33 33
 	 * @since   11.4
34 34
 	 */
35  
-	public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null);
  35
+	public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null);
36 36
 }
16  libraries/joomla/http/transports/curl.php
@@ -39,7 +39,7 @@ public function __construct()
39 39
 	 *
40 40
 	 * @param   string   $method     The HTTP method for sending the request.
41 41
 	 * @param   JUri     $uri        The URI to the resource to request.
42  
-	 * @param   array    $data       An array of key => value pairs to send with the request.
  42
+	 * @param   mixed    $data       Either an associative array or a string to be sent with the request.
43 43
 	 * @param   array    $headers    An array of request headers to send with the request.
44 44
 	 * @param   integer  $timeout    Read timeout in seconds.
45 45
 	 * @param   string   $userAgent  The optional user agent string to send with the request.
@@ -48,7 +48,7 @@ public function __construct()
48 48
 	 *
49 49
 	 * @since   11.4
50 50
 	 */
51  
-	public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null)
  51
+	public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
52 52
 	{
53 53
 		// Setup the cURL handle.
54 54
 		$ch = curl_init();
@@ -59,8 +59,16 @@ public function request($method, JUri $uri, array $data = null, array $headers =
59 59
 		// If data exists let's encode it and make sure our Content-type header is set.
60 60
 		if (isset($data))
61 61
 		{
62  
-			// Add the encoded content into the stream context options array.
63  
-			$options[CURLOPT_POSTFIELDS] = http_build_query($data);
  62
+			// If the data is a scalar value simply add it to the cURL post fields.
  63
+			if (is_scalar($data))
  64
+			{
  65
+				$options[CURLOPT_POSTFIELDS] = $data;
  66
+			}
  67
+			// Otherwise we need to encode the value first.
  68
+			else
  69
+			{
  70
+				$options[CURLOPT_POSTFIELDS] = http_build_query($data);
  71
+			}
64 72
 
65 73
 			if (!isset($headers['Content-type']))
66 74
 			{
8  libraries/joomla/http/transports/socket.php
@@ -45,7 +45,7 @@ public function __construct()
45 45
 	 *
46 46
 	 * @param   string   $method     The HTTP method for sending the request.
47 47
 	 * @param   JUri     $uri        The URI to the resource to request.
48  
-	 * @param   array    $data       An array of key => value pairs to send with the request.
  48
+	 * @param   mixed    $data       Either an associative array or a string to be sent with the request.
49 49
 	 * @param   array    $headers    An array of request headers to send with the request.
50 50
 	 * @param   integer  $timeout    Read timeout in seconds.
51 51
 	 * @param   string   $userAgent  The optional user agent string to send with the request.
@@ -55,7 +55,7 @@ public function __construct()
55 55
 	 * @since   11.4
56 56
 	 * @throws  RuntimeException
57 57
 	 */
58  
-	public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null)
  58
+	public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
59 59
 	{
60 60
 		$connection = $this->connect($uri, $timeout);
61 61
 
@@ -80,8 +80,8 @@ public function request($method, JUri $uri, array $data = null, array $headers =
80 80
 		// If we have data to send make sure our request is setup for it.
81 81
 		if (!empty($data))
82 82
 		{
83  
-			// If the data is an array, build the request query string.
84  
-			if (is_array($data))
  83
+			// If the data is not a scalar value encode it to be sent with the request.
  84
+			if (!is_scalar($data))
85 85
 			{
86 86
 				$data = http_build_query($data);
87 87
 			}
16  libraries/joomla/http/transports/stream.php
@@ -46,7 +46,7 @@ public function __construct()
46 46
 	 *
47 47
 	 * @param   string   $method     The HTTP method for sending the request.
48 48
 	 * @param   JUri     $uri        The URI to the resource to request.
49  
-	 * @param   array    $data       An array of key => value pairs to send with the request.
  49
+	 * @param   mixed    $data       Either an associative array or a string to be sent with the request.
50 50
 	 * @param   array    $headers    An array of request headers to send with the request.
51 51
 	 * @param   integer  $timeout    Read timeout in seconds.
52 52
 	 * @param   string   $userAgent  The optional user agent string to send with the request.
@@ -55,7 +55,7 @@ public function __construct()
55 55
 	 *
56 56
 	 * @since   11.4
57 57
 	 */
58  
-	public function request($method, JUri $uri, array $data = null, array $headers = null, $timeout = null, $userAgent = null)
  58
+	public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
59 59
 	{
60 60
 		// Create the stream context options array with the required method offset.
61 61
 		$options = array('method' => strtoupper($method));
@@ -63,8 +63,16 @@ public function request($method, JUri $uri, array $data = null, array $headers =
63 63
 		// If data exists let's encode it and make sure our Content-type header is set.
64 64
 		if (isset($data))
65 65
 		{
66  
-			// Add the encoded content into the stream context options array.
67  
-			$options['content'] = http_build_query($data);
  66
+			// If the data is a scalar value simply add it to the stream context options.
  67
+			if (is_scalar($data))
  68
+			{
  69
+				$options['content'] = $data;
  70
+			}
  71
+			// Otherwise we need to encode the value first.
  72
+			else
  73
+			{
  74
+				$options['content'] = http_build_query($data);
  75
+			}
68 76
 
69 77
 			if (!isset($headers['Content-type']))
70 78
 			{

0 notes on commit 649e5cc

Please sign in to comment.
Something went wrong with that request. Please try again.