Skip to content

Commit 033af75

Browse files
committed
- url params generation
1 parent 1f2b0ff commit 033af75

4 files changed

Lines changed: 35 additions & 69 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=5.3.0",
14+
"php": ">=5.4.0",
1515
"lib-curl": "*"
1616
},
1717
"suggest": {

dHttp/Client.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Client
1717
CURLOPT_RETURNTRANSFER => true,
1818
CURLOPT_FOLLOWLOCATION => false,
1919
CURLOPT_SSL_VERIFYPEER => false,
20-
CURLOPT_USERAGENT => 'dHttp'
20+
CURLOPT_USERAGENT => 'PHP dHttp/Client 1.3'
2121
];
2222
/**
2323
* @var array
@@ -57,7 +57,7 @@ public function __construct($url = null, array $options = [])
5757
public function setUrl($url)
5858
{
5959
if ($url !== null) {
60-
$this->_options[CURLOPT_URL] = Url::validateUrl($url);
60+
$this->_options[CURLOPT_URL] = $this->prepareUrl($url);
6161
}
6262

6363
return $this;
@@ -176,7 +176,10 @@ public function addOptions(array $params)
176176
*/
177177
public function post($fields = [], array $options = [])
178178
{
179-
return $this->get($options + [CURLOPT_POST => true, CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields]);
179+
return $this->get($options + [
180+
CURLOPT_POST => true,
181+
CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields
182+
]);
180183
}
181184

182185
/**
@@ -188,7 +191,10 @@ public function post($fields = [], array $options = [])
188191
*/
189192
public function put($fields = [], array $options = [])
190193
{
191-
return $this->get($options + [CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields]);
194+
return $this->get($options + [
195+
CURLOPT_CUSTOMREQUEST => 'PUT',
196+
CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields
197+
]);
192198
}
193199

194200
/**
@@ -324,6 +330,25 @@ public function reset()
324330
return $this;
325331
}
326332

333+
/**
334+
* @param $url
335+
* @return string
336+
*/
337+
public function prepareUrl($url)
338+
{
339+
if (is_array($url) && count($url)) {
340+
$newUrl = $url[0];
341+
342+
if (isset($url[1]) && is_array($url[1])) {
343+
$newUrl = '?' . http_build_query($url[1]);
344+
}
345+
} else {
346+
$newUrl = $url;
347+
}
348+
349+
return $newUrl;
350+
}
351+
327352
/**
328353
* Return curl information
329354
*

dHttp/Url.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ The recommended way to install library is [through composer](http://getcomposer.
2727
```php
2828
required __DIR__ . '/vendor/autoload.php';
2929

30-
$client = new dHttp\Client('http://website.com');
30+
// http://website.com?param1=value1
31+
$client = new dHttp\Client(['http://website.com', [
32+
'param1' => 'value1'
33+
]], [CURLOPT_TIMEOUT => 5]);
3134

3235
$resp = $client->get();
3336
// Get response code
@@ -48,7 +51,7 @@ var_dump($resp->getHeader('Content-Type'));
4851
required __DIR__ . '/vendor/autoload.php';
4952

5053
$client = new dHttp\Client('http://website.com');
51-
$client->addOptions(array(CURLOPT_RETURNTRANSFER => false))
54+
$client->addOptions([CURLOPT_RETURNTRANSFER => false])
5255
->setCookie('/tmp/cookie.txt')
5356
->setUserAgent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31')
5457
->post(array(

0 commit comments

Comments
 (0)