Skip to content

Commit

Permalink
新增withUA()方法
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Mar 22, 2020
1 parent fcbe391 commit f1ea00c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $response = Http::head('http://httpbin.org/head');
$response = Http::options('http://httpbin.org/options');
```

###### 发送 URL 编码的请求
###### 发送 URL 编码请求

```php
// application/json(默认)
Expand Down Expand Up @@ -101,6 +101,11 @@ $response = Http::withBasicAuth('username', 'password')->post(...);
$response = Http::withDigestAuth('username', 'password')->post(...);
```

###### 携带 User-Agent 的请求
```php
$response = Http::withUA('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36')->post(...);
```

###### 携带Token令牌的请求

```php
Expand Down Expand Up @@ -163,7 +168,6 @@ $response = Http::delay(60)->post(...);
```php
$response = Http::concurrency(10)->promise(...);
```
> 此方法仅在并发请求时生效

#### 异步请求

Expand Down
1 change: 1 addition & 0 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @method static \Gouguoyin\EasyHttp\Request withHeaders(array $headers)
* @method static \Gouguoyin\EasyHttp\Request withBasicAuth(string $username, string $password)
* @method static \Gouguoyin\EasyHttp\Request withDigestAuth(string $username, string $password)
* @method static \Gouguoyin\EasyHttp\Request withUA(string $ua)
* @method static \Gouguoyin\EasyHttp\Request withToken(string $token, string $type = 'Bearer')
* @method static \Gouguoyin\EasyHttp\Request withCookies(array $cookies, string $domain)
* @method static \Gouguoyin\EasyHttp\Request withProxy(string|array $proxy)
Expand Down
9 changes: 8 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ public function withDigestAuth(string $username, string $password)
return $this;
}

public function withUA(string $ua)
{
$this->options['headers']['User-Agent'] = trim($ua);

return $this;
}

public function withToken(string $token, string $type = 'Bearer')
{
$this->options['headers']['Authorization'] = trim($type.' '.$token);
$this->options['headers']['Authorization'] = trim($type . ' ' . $token);

return $this;
}
Expand Down

0 comments on commit f1ea00c

Please sign in to comment.