Skip to content

Commit

Permalink
complete fsock client.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 23, 2018
1 parent 8c79bd6 commit 03a1ea0
Show file tree
Hide file tree
Showing 13 changed files with 602 additions and 229 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

PHP http client library.

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

## Install
## 安装

```bash
composer require php-comp/http-client
```

## Usage
## 使用

### CURL

- simple
- 简单使用

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

- file upload/download
- 文件上传下载

```text
public function upload(string $url, string $field, string $filePath, string $mimeType = '')
Expand Down
68 changes: 63 additions & 5 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ abstract class AbstractClient implements ClientInterface
protected $error = '';

/**
* @var int response status code
* @var int response status code. eg. 200 404
*/
protected $statusCode = 200;
protected $statusCode = 0;

/**
* @var string body string, it's parsed from $_response
Expand Down Expand Up @@ -179,6 +179,21 @@ public static function getSupportedMethods()
return self::$supportedMethods;
}

/**
* @param string $method
* @return string
*/
protected function formatAndCheckMethod(string $method): string
{
$method = \strtoupper($method);

if (!isset(self::$supportedMethods[$method])) {
throw new \InvalidArgumentException("The method type [$method] is not supported!");
}

return $method;
}

/**************************************************************************
* request methods
*************************************************************************/
Expand Down Expand Up @@ -262,7 +277,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
}

// send request
$this->request($request->getRequestTarget(), $request->getBody(), $request->getMethod());
$this->request($request->getRequestTarget(), (string)$request->getBody(), $request->getMethod());

return $this->getPsr7Response();
}
Expand Down Expand Up @@ -437,8 +452,10 @@ public function addHeaders(array $headers, bool $override = true)
*/
public function setHeader(string $name, string $value, bool $override = false)
{
$name = \ucwords($name);

if ($override || !isset($this->headers[$name])) {
$this->headers[$name] = \ucwords($name) . ": $value";
$this->headers[$name] = $value;
}

return $this;
Expand Down Expand Up @@ -679,7 +696,6 @@ public function isDebug(): bool
public function setDebug($debug)
{
$this->options['debug'] = (bool)$debug;

return $this;
}

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

/**
* @return bool|array
*/
public function getArrayData()
{
return $this->getJsonArray();
}

/**
* @return bool|array
*/
public function getJsonArray()
{
if (!$body = $this->getResponseBody()) {
return [];
}

$data = \json_decode($body, true);
if (\json_last_error() > 0) {
return false;
}

return $data;
}

/**
* @return bool|\stdClass
*/
public function getJsonObject()
{
if (!$body = $this->getResponseBody()) {
return false;
}

$data = \json_decode($body);
if (\json_last_error() > 0) {
return false;
}

return $data;
}

/**
* @return string
*/
Expand Down
38 changes: 38 additions & 0 deletions src/ClientUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace PhpComp\Http\Client;

use PhpComp\Http\Client\Error\ClientException;

/**
* Class ClientUtil
* @package PhpComp\Http\Client
Expand Down Expand Up @@ -36,6 +38,20 @@ public static function mergeArray(array $src, array $append): array
return $src;
}

/**
* @param array $arr
* @return array
*/
public static function ucwordArrayKeys(array $arr): array
{
$newMap = [];
foreach ($arr as $key => $value) {
$newMap[\ucwords($key)] = $value;
}

return $newMap;
}

/**
* @param string $url
* @return bool
Expand All @@ -45,6 +61,28 @@ public static function isFullURL(string $url): bool
return 0 === \strpos($url, 'http:') || 0 === \strpos($url, 'https:') || 0 === strpos($url, '//');
}

/**
* @param string $url
* @return array
*/
public static function parseUrl(string $url): array
{
$info = \parse_url($url);
if ($info === false) {
throw new ClientException('invalid request url: ' . $url);
}

$info = \array_merge([
'scheme' => 'http',
'host' => '',
'port' => 80,
'path' => '/',
'query' => '',
], $info);

return $info;
}

/**
* @param string $url
* @param array|object $data
Expand Down
Loading

0 comments on commit 03a1ea0

Please sign in to comment.