Skip to content

Commit

Permalink
- url params generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ARACOOOL committed Nov 14, 2015
1 parent 1f2b0ff commit 033af75
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 69 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"lib-curl": "*"
},
"suggest": {
Expand Down
33 changes: 29 additions & 4 deletions dHttp/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Client
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'dHttp'
CURLOPT_USERAGENT => 'PHP dHttp/Client 1.3'
];
/**
* @var array
Expand Down Expand Up @@ -57,7 +57,7 @@ public function __construct($url = null, array $options = [])
public function setUrl($url)
{
if ($url !== null) {
$this->_options[CURLOPT_URL] = Url::validateUrl($url);
$this->_options[CURLOPT_URL] = $this->prepareUrl($url);
}

return $this;
Expand Down Expand Up @@ -176,7 +176,10 @@ public function addOptions(array $params)
*/
public function post($fields = [], array $options = [])
{
return $this->get($options + [CURLOPT_POST => true, CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields]);
return $this->get($options + [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields
]);
}

/**
Expand All @@ -188,7 +191,10 @@ public function post($fields = [], array $options = [])
*/
public function put($fields = [], array $options = [])
{
return $this->get($options + [CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields]);
return $this->get($options + [
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => is_array($fields) ? http_build_query($fields) : $fields
]);
}

/**
Expand Down Expand Up @@ -324,6 +330,25 @@ public function reset()
return $this;
}

/**
* @param $url
* @return string
*/
public function prepareUrl($url)
{
if (is_array($url) && count($url)) {
$newUrl = $url[0];

if (isset($url[1]) && is_array($url[1])) {
$newUrl = '?' . http_build_query($url[1]);
}
} else {
$newUrl = $url;
}

return $newUrl;
}

/**
* Return curl information
*
Expand Down
62 changes: 0 additions & 62 deletions dHttp/Url.php

This file was deleted.

7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ The recommended way to install library is [through composer](http://getcomposer.
```php
required __DIR__ . '/vendor/autoload.php';

$client = new dHttp\Client('http://website.com');
// http://website.com?param1=value1
$client = new dHttp\Client(['http://website.com', [
'param1' => 'value1'
]], [CURLOPT_TIMEOUT => 5]);

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

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

0 comments on commit 033af75

Please sign in to comment.