-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from gaobinzhan/master
http-client
- Loading branch information
Showing
12 changed files
with
943 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* @author gaobinzhan <[email protected]> | ||
*/ | ||
|
||
namespace EasySwoole\HttpClient\Contract; | ||
|
||
use EasySwoole\HttpClient\Handler\AbstractRequest; | ||
|
||
/** | ||
* Interface ClientInterface | ||
*/ | ||
interface ClientInterface | ||
{ | ||
/** | ||
* ClientInterface constructor. | ||
* @param string|null $url | ||
*/ | ||
public function __construct(?string $url = null); | ||
|
||
/** | ||
* create client | ||
* @param string $host | ||
* @param int $port | ||
* @param bool $ssl | ||
* @return mixed | ||
*/ | ||
public function createClient(string $host, int $port = 80, bool $ssl = false); | ||
|
||
/** | ||
* get client | ||
* @return mixed | ||
*/ | ||
public function getClient(); | ||
|
||
/** | ||
* close client | ||
* @return mixed | ||
*/ | ||
public function closeClient(); | ||
|
||
/** | ||
* @return AbstractRequest | ||
*/ | ||
public function getRequest(): AbstractRequest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* @author gaobinzhan <[email protected]> | ||
*/ | ||
|
||
|
||
namespace EasySwoole\HttpClient\Handler; | ||
|
||
|
||
use EasySwoole\HttpClient\Contract\ClientInterface; | ||
use EasySwoole\HttpClient\Handler\Swoole\Request; | ||
use EasySwoole\HttpClient\Traits\UriManager; | ||
|
||
abstract class AbstractClient implements ClientInterface | ||
{ | ||
|
||
use UriManager; | ||
|
||
/** | ||
* @var ClientInterface | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* @var AbstractRequest | ||
*/ | ||
protected $request; | ||
|
||
|
||
public function __construct(?string $url = null) | ||
{ | ||
if (!empty($url)) { | ||
$this->setUrl($url); | ||
} | ||
} | ||
|
||
public function getRequest(): AbstractRequest | ||
{ | ||
if (!$this->request instanceof AbstractRequest) { | ||
$this->request = new Request(); | ||
} | ||
return $this->request; | ||
} | ||
|
||
|
||
public function setQuery(?array $data) | ||
{ | ||
if ($data) { | ||
$old = $this->url->getQuery(); | ||
parse_str($old, $old); | ||
$this->url->setQuery(http_build_query($data + $old)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
/** | ||
* @author gaobinzhan <[email protected]> | ||
*/ | ||
|
||
namespace EasySwoole\HttpClient\Handler; | ||
|
||
use EasySwoole\HttpClient\HttpClient; | ||
|
||
abstract class AbstractRequest | ||
{ | ||
|
||
/** | ||
* 请求携带的Cookies | ||
* @var array | ||
*/ | ||
protected $cookies = []; | ||
|
||
|
||
/** | ||
* 默认请求头 | ||
* @var array | ||
*/ | ||
protected $header = [ | ||
"user-agent" => 'EasySwooleHttpClient/0.1', | ||
'accept' => '*/*', | ||
'pragma' => 'no-cache', | ||
'cache-control' => 'no-cache' | ||
]; | ||
|
||
protected $followLocation = 3; | ||
|
||
protected $redirected = 0; | ||
|
||
/** | ||
* 请求方法 | ||
* @var string | ||
*/ | ||
protected $method = HttpClient::METHOD_GET; | ||
|
||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getFollowLocation(): int | ||
{ | ||
return $this->followLocation; | ||
} | ||
|
||
/** | ||
* @param int $followLocation | ||
* @return int | ||
*/ | ||
public function setFollowLocation(int $followLocation): int | ||
{ | ||
$this->followLocation = $followLocation; | ||
return $this->followLocation; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getRedirected(): int | ||
{ | ||
return $this->redirected; | ||
} | ||
|
||
/** | ||
* @param int $redirected | ||
*/ | ||
public function setRedirected(int $redirected): void | ||
{ | ||
$this->redirected = $redirected; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getHeader(): array | ||
{ | ||
return $this->header; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMethod(): string | ||
{ | ||
return $this->method; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getCookies(): array | ||
{ | ||
return $this->cookies; | ||
} | ||
|
||
/** | ||
* @param array $cookies | ||
*/ | ||
public function setCookies(array $cookies): void | ||
{ | ||
$this->cookies = $cookies; | ||
} | ||
|
||
|
||
public function setHeaders(array $header, $isMerge = true, $strtolower = true) | ||
{ | ||
if (empty($header)) { | ||
return; | ||
} | ||
|
||
// 非合并模式先清空当前的Header再设置 | ||
if (!$isMerge) { | ||
$this->header = []; | ||
} | ||
|
||
foreach ($header as $name => $value) { | ||
$this->setHeader($name, $value, $strtolower); | ||
} | ||
} | ||
|
||
|
||
public function setBasicAuth(string $userName, string $password) | ||
{ | ||
$basicAuthToken = base64_encode("{$userName}:{$password}"); | ||
$this->setHeader('Authorization', "Basic {$basicAuthToken}", false); | ||
} | ||
|
||
public function setHeader(string $key, string $value, $strtolower = true) | ||
{ | ||
if ($strtolower) { | ||
$this->header[strtolower($key)] = strtolower($value); | ||
} else { | ||
$this->header[$key] = $value; | ||
} | ||
} | ||
|
||
public function setMethod(string $method) | ||
{ | ||
$this->method = $method; | ||
} | ||
|
||
public function setContentType(string $contentType) | ||
{ | ||
$this->setHeader('content-type', $contentType); | ||
} | ||
|
||
public function addCookie(string $key, string $value) | ||
{ | ||
$this->cookies[$key] = $value; | ||
} | ||
|
||
public function addCookies(array $cookies, $isMerge = true) | ||
{ | ||
|
||
if ($isMerge) { // 合并配置项到当前配置中 | ||
foreach ($cookies as $name => $value) { | ||
$this->cookies[$name] = $value; | ||
} | ||
} else { | ||
$this->cookies = $cookies; | ||
} | ||
} | ||
} |
Oops, something went wrong.