Skip to content

Commit

Permalink
Add options method, fix the content-type bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Apr 6, 2018
1 parent de09ad2 commit 8756648
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,40 @@ go(function () {

------

## Saber API

为了使用方便,已为所有支持的请求方法提供了别名。

**\$saber->request(\$options)**

**\$saber->get(\$url[, \$options])**

**\$saber->delete(\$url[, \$options])**

**\$saber->head(\$url[, \$options])**

**\$saber->options(\$url[, \$options])**

**\$saber->post(\$url[, \$data[, \$options]])**

**\$saber->put(\$url[, \$data[, \$options]])**

**\$saber->patch(\$url[, \$data[, \$options]])**

<br>

------

## 例子

### 简单请求
### 静态方法

```php
Saber::get('http://httpbin.org/get');
Saber::post('http://httpbin.org/post');
Saber::put('http://httpbin.org/put');
Saber::patch('http://httpbin.org/patch');
Saber::delete('http://httpbin.org/delete');
Saber::post('http://httpbin.org/post', ['foo' => 'bar']);
Saber::put('http://httpbin.org/put', ['foo' => 'bar']);
Saber::patch('http://httpbin.org/patch', ['foo' => 'bar']);
```

### 生成实例
Expand All @@ -83,16 +107,17 @@ Saber::delete('http://httpbin.org/delete');
$saber = Saber::create([
'base_uri' => 'http://httpbin.org',
'headers' => [
'User-Agent' => null,
'Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8',
'DNT' => '1'
],
'Content-Type' => ContentType::JSON,
'DNT' => '1',
'User-Agent' => null
]
]);
echo $saber->get('/get');
echo $saber->post('/post');
echo $saber->patch('/patch');
echo $saber->put('/put');
echo $saber->delete('/delete');
echo $saber->post('/post', ['foo' => 'bar']);
echo $saber->patch('/patch', ['foo' => 'bar']);
echo $saber->put('/put', ['foo' => 'bar']);
```

### 生成会话
Expand Down
14 changes: 8 additions & 6 deletions examples/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Date: 2018/3/23 下午8:50
*/

use Swlib\Http\ContentType;
use Swlib\Saber;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -13,14 +14,15 @@
$saber = Saber::create([
'base_uri' => 'http://httpbin.org',
'headers' => [
'User-Agent' => null,
'Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8',
'DNT' => '1'
],
'Content-Type' => ContentType::JSON,
'DNT' => '1',
'User-Agent' => null
]
]);
echo $saber->get('/get');
echo $saber->post('/post');
echo $saber->patch('/patch');
echo $saber->put('/put');
echo $saber->delete('/delete');
echo $saber->post('/post', ['foo' => 'bar']);
echo $saber->patch('/patch', ['foo' => 'bar']);
echo $saber->put('/put', ['foo' => 'bar']);
});
12 changes: 10 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ public static function transOptionsToRequest(array $options, Request $request)

/** 设置请求的数据 */
if (isset($options['content_type'])) {
$request->withHeader('Content-type', $options['content_type']);
$request->withHeader('Content-Type', $options['content_type']);
}
if (!empty($options['data'])) {
if (!is_string($options['data'])) {
switch ($options['content_type']) {
switch ($request->getHeaderLine('Content-Type')) {
case ContentType::JSON:
$options['data'] = json_encode($options['data']);
break;
Expand Down Expand Up @@ -450,6 +450,14 @@ public function head(string $uri, array $options = [])
return $this->request($options);
}

public function options(string $uri, array $options = [])
{
$options['uri'] = $uri;
$options['method'] = 'OPTIONS';

return $this->request($options);
}

public function post(string $uri, $data = null, array $options = [])
{
$options['uri'] = $uri;
Expand Down
12 changes: 6 additions & 6 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ public function withRedirect(int $time): self
return $this;
}

/** @return bool */
public function getRedirectWait(): bool
{
return $this->redirect_wait;
}

/**
* @param bool $enable
* @return $this
Expand All @@ -308,12 +314,6 @@ public function withRedirectWait(bool $enable): self
return $this;
}

/** @return bool */
public function getRedirectWait(): bool
{
return $this->redirect_wait;
}

/**
* Clear the swoole client to make it back to the first state.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Saber.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public static function head(string $uri, array $options = [])
return self::getDefaultClient()->head($uri, $options);
}

public static function options(string $uri, array $options = [])
{
return self::getDefaultClient()->options($uri, $options);
}

public static function post(string $uri, $data = null, array $options = [])
{
return self::getDefaultClient()->post($uri, $data, $options);
Expand Down

0 comments on commit 8756648

Please sign in to comment.