Skip to content

Commit

Permalink
Merge pull request #26 from gaobinzhan/master
Browse files Browse the repository at this point in the history
http-client
  • Loading branch information
kiss291323003 authored Jul 12, 2020
2 parents 2e1a163 + 2f2d250 commit f38a116
Show file tree
Hide file tree
Showing 12 changed files with 943 additions and 336 deletions.
42 changes: 42 additions & 0 deletions src/Bean/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,46 @@ public function setClient($client): void
{
$this->client = $client;
}

/**
* 获取json格式的内容
* @param bool $assoc true返回数组 false返回对象
* @return mixed
*/
public function json($assoc = false)
{
return json_decode($this->body, $assoc);
}

/**
* 获取jsonp格式的内容
* @param bool $assoc true返回数组 false返回对象
* @return mixed
*/
public function jsonp($assoc = false)
{
$jsonp = trim($this->body);
if (isset($jsonp[0]) && $jsonp[0] !== '[' && $jsonp[0] !== '{') {
$begin = strpos($jsonp, '(');
$end = strrpos($jsonp, ')');
if (false !== $begin && false !== $end) {
$jsonp = substr($jsonp, $begin + 1, $end - $begin - 1);
}
}
return json_decode($jsonp, $assoc);
}

/**
* 获取xml格式的内容
* @param bool $assoc true返回数组 false返回对象
* @return array|object
*/
public function xml($assoc = false)
{
$xml = simplexml_load_string($this->body, null, LIBXML_NOCDATA | LIBXML_COMPACT);
if ($assoc) {
$xml = (array)$xml;
}
return $xml;
}
}
46 changes: 46 additions & 0 deletions src/Contract/ClientInterface.php
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;
}
54 changes: 54 additions & 0 deletions src/Handler/AbstractClient.php
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));
}
}
}
167 changes: 167 additions & 0 deletions src/Handler/AbstractRequest.php
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;
}
}
}
Loading

0 comments on commit f38a116

Please sign in to comment.