Skip to content

Commit 658a10f

Browse files
committed
Fix php-curl-class#441: Allow using string as well as array for query parameter argument
1 parent 5d1a9e4 commit 658a10f

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ Curl::setPort($port)
238238
Curl::setReferer($referer)
239239
Curl::setReferrer($referrer)
240240
Curl::setTimeout($seconds)
241-
Curl::setUrl($url, $data = array())
241+
Curl::setUrl($url, $mixed_data = '')
242242
Curl::setUserAgent($user_agent)
243243
Curl::setXmlDecoder($function)
244244
Curl::success($callback)

src/Curl/Curl.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,12 +1114,12 @@ public function setTimeout($seconds)
11141114
*
11151115
* @access public
11161116
* @param $url
1117-
* @param $data
1117+
* @param $mixed_data
11181118
*/
1119-
public function setUrl($url, $data = array())
1119+
public function setUrl($url, $mixed_data = '')
11201120
{
11211121
$this->baseUrl = $url;
1122-
$this->url = $this->buildURL($url, $data);
1122+
$this->url = $this->buildURL($url, $mixed_data);
11231123
$this->setOpt(CURLOPT_URL, $this->url);
11241124
}
11251125

@@ -1259,13 +1259,21 @@ private function __get_totalTime()
12591259
*
12601260
* @access private
12611261
* @param $url
1262-
* @param $data
1262+
* @param $mixed_data
12631263
*
12641264
* @return string
12651265
*/
1266-
private function buildURL($url, $data = array())
1266+
private function buildURL($url, $mixed_data = '')
12671267
{
1268-
return $url . (empty($data) ? '' : '?' . http_build_query($data, '', '&'));
1268+
$query_string = '';
1269+
if (!empty($mixed_data)) {
1270+
if (is_string($mixed_data)) {
1271+
$query_string .= '?' . $mixed_data;
1272+
} elseif (is_array($mixed_data)) {
1273+
$query_string .= '?' . http_build_query($mixed_data, '', '&');
1274+
}
1275+
}
1276+
return $url . $query_string;
12691277
}
12701278

12711279
/**

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,63 @@ public function testOptionSet()
30233023
$this->assertTrue($curl->getOpt(CURLOPT_VERBOSE));
30243024
}
30253025

3026+
public function testBuildUrlArgs()
3027+
{
3028+
$tests = array(
3029+
array(
3030+
'args' => array(
3031+
'url' => 'https://www.example.com/',
3032+
'mixed_data' => null,
3033+
),
3034+
'expected' => 'https://www.example.com/',
3035+
),
3036+
array(
3037+
'args' => array(
3038+
'url' => 'https://www.example.com/',
3039+
'mixed_data' => '',
3040+
),
3041+
'expected' => 'https://www.example.com/',
3042+
),
3043+
array(
3044+
'args' => array(
3045+
'url' => 'https://www.example.com/',
3046+
'mixed_data' => array(),
3047+
),
3048+
'expected' => 'https://www.example.com/',
3049+
),
3050+
array(
3051+
'args' => array(
3052+
'url' => 'https://www.example.com/',
3053+
'mixed_data' => array(
3054+
'a' => '1',
3055+
'b' => '2',
3056+
'c' => '3',
3057+
),
3058+
),
3059+
'expected' => 'https://www.example.com/?a=1&b=2&c=3',
3060+
),
3061+
array(
3062+
'args' => array(
3063+
'url' => 'https://www.example.com/',
3064+
'mixed_data' => 'user_ids=user_1,user_2',
3065+
),
3066+
'expected' => 'https://www.example.com/?user_ids=user_1,user_2',
3067+
),
3068+
);
3069+
foreach ($tests as $test) {
3070+
$curl_1 = new Curl();
3071+
$reflector = new ReflectionObject($curl_1);
3072+
$method = $reflector->getMethod('buildURL');
3073+
$method->setAccessible(true);
3074+
$actual_url = $method->invoke($curl_1, $test['args']['url'], $test['args']['mixed_data']);
3075+
$this->assertEquals($test['expected'], $actual_url);
3076+
3077+
$curl_2 = new Curl();
3078+
$curl_2->setUrl($test['args']['url'], $test['args']['mixed_data']);
3079+
$this->assertEquals($test['expected'], $curl_2->url);
3080+
}
3081+
}
3082+
30263083
public function testBuildUrlArgSeparator()
30273084
{
30283085
$base_url = 'https://www.example.com/path';

0 commit comments

Comments
 (0)