Skip to content

Commit 03a1ea0

Browse files
committed
complete fsock client.
1 parent 8c79bd6 commit 03a1ea0

File tree

13 files changed

+602
-229
lines changed

13 files changed

+602
-229
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66

77
PHP http client library.
88

9-
- driver contains: `curl` `swoole`
10-
- implement the [PSR 18](https://github.com/php-fig/http-client)
9+
- 可用的驱动包括: `curl` `swoole` `fsockopen`
10+
- 实现接口 [PSR 18](https://github.com/php-fig/http-client)
1111

12-
## Install
12+
## 安装
1313

1414
```bash
1515
composer require php-comp/http-client
1616
```
1717

18-
## Usage
18+
## 使用
1919

2020
### CURL
2121

22-
- simple
22+
- 简单使用
2323

2424
```php
2525
use PhpComp\Http\Client\Curl\Curl;
@@ -40,7 +40,7 @@ $curl->reset()->post('/users/1', $post);
4040
$array = $curl->getArrayData();
4141
```
4242

43-
- file upload/download
43+
- 文件上传下载
4444

4545
```text
4646
public function upload(string $url, string $field, string $filePath, string $mimeType = '')

src/AbstractClient.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ abstract class AbstractClient implements ClientInterface
120120
protected $error = '';
121121

122122
/**
123-
* @var int response status code
123+
* @var int response status code. eg. 200 404
124124
*/
125-
protected $statusCode = 200;
125+
protected $statusCode = 0;
126126

127127
/**
128128
* @var string body string, it's parsed from $_response
@@ -179,6 +179,21 @@ public static function getSupportedMethods()
179179
return self::$supportedMethods;
180180
}
181181

182+
/**
183+
* @param string $method
184+
* @return string
185+
*/
186+
protected function formatAndCheckMethod(string $method): string
187+
{
188+
$method = \strtoupper($method);
189+
190+
if (!isset(self::$supportedMethods[$method])) {
191+
throw new \InvalidArgumentException("The method type [$method] is not supported!");
192+
}
193+
194+
return $method;
195+
}
196+
182197
/**************************************************************************
183198
* request methods
184199
*************************************************************************/
@@ -262,7 +277,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
262277
}
263278

264279
// send request
265-
$this->request($request->getRequestTarget(), $request->getBody(), $request->getMethod());
280+
$this->request($request->getRequestTarget(), (string)$request->getBody(), $request->getMethod());
266281

267282
return $this->getPsr7Response();
268283
}
@@ -437,8 +452,10 @@ public function addHeaders(array $headers, bool $override = true)
437452
*/
438453
public function setHeader(string $name, string $value, bool $override = false)
439454
{
455+
$name = \ucwords($name);
456+
440457
if ($override || !isset($this->headers[$name])) {
441-
$this->headers[$name] = \ucwords($name) . ": $value";
458+
$this->headers[$name] = $value;
442459
}
443460

444461
return $this;
@@ -679,7 +696,6 @@ public function isDebug(): bool
679696
public function setDebug($debug)
680697
{
681698
$this->options['debug'] = (bool)$debug;
682-
683699
return $this;
684700
}
685701

@@ -701,6 +717,48 @@ public function __toString(): string
701717
return $this->getResponseBody();
702718
}
703719

720+
/**
721+
* @return bool|array
722+
*/
723+
public function getArrayData()
724+
{
725+
return $this->getJsonArray();
726+
}
727+
728+
/**
729+
* @return bool|array
730+
*/
731+
public function getJsonArray()
732+
{
733+
if (!$body = $this->getResponseBody()) {
734+
return [];
735+
}
736+
737+
$data = \json_decode($body, true);
738+
if (\json_last_error() > 0) {
739+
return false;
740+
}
741+
742+
return $data;
743+
}
744+
745+
/**
746+
* @return bool|\stdClass
747+
*/
748+
public function getJsonObject()
749+
{
750+
if (!$body = $this->getResponseBody()) {
751+
return false;
752+
}
753+
754+
$data = \json_decode($body);
755+
if (\json_last_error() > 0) {
756+
return false;
757+
}
758+
759+
return $data;
760+
}
761+
704762
/**
705763
* @return string
706764
*/

src/ClientUtil.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace PhpComp\Http\Client;
1010

11+
use PhpComp\Http\Client\Error\ClientException;
12+
1113
/**
1214
* Class ClientUtil
1315
* @package PhpComp\Http\Client
@@ -36,6 +38,20 @@ public static function mergeArray(array $src, array $append): array
3638
return $src;
3739
}
3840

41+
/**
42+
* @param array $arr
43+
* @return array
44+
*/
45+
public static function ucwordArrayKeys(array $arr): array
46+
{
47+
$newMap = [];
48+
foreach ($arr as $key => $value) {
49+
$newMap[\ucwords($key)] = $value;
50+
}
51+
52+
return $newMap;
53+
}
54+
3955
/**
4056
* @param string $url
4157
* @return bool
@@ -45,6 +61,28 @@ public static function isFullURL(string $url): bool
4561
return 0 === \strpos($url, 'http:') || 0 === \strpos($url, 'https:') || 0 === strpos($url, '//');
4662
}
4763

64+
/**
65+
* @param string $url
66+
* @return array
67+
*/
68+
public static function parseUrl(string $url): array
69+
{
70+
$info = \parse_url($url);
71+
if ($info === false) {
72+
throw new ClientException('invalid request url: ' . $url);
73+
}
74+
75+
$info = \array_merge([
76+
'scheme' => 'http',
77+
'host' => '',
78+
'port' => 80,
79+
'path' => '/',
80+
'query' => '',
81+
], $info);
82+
83+
return $info;
84+
}
85+
4886
/**
4987
* @param string $url
5088
* @param array|object $data

0 commit comments

Comments
 (0)