Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
yf committed Dec 25, 2018
0 parents commit 6b9ec00
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 0 deletions.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "easyswoole/http-client",
"type": "library",
"description": "php stander lib",
"keywords" : ["swoole", "framework", "async","easyswoole"],
"homepage" : "https://www.easyswoole.com/",
"license" : "Apache-2.0",
"authors": [
{
"name": "YF",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.1.0",
"easyswoole/spl": "^1.1",
"easyswoole/swoole-ide-helper": "^1.03"
},
"autoload": {
"psr-4": {
"EasySwoole\\HttpClient\\": "src/"
}
}
}
102 changes: 102 additions & 0 deletions src/Bean/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/12/25
* Time: 10:49 AM
*/

namespace EasySwoole\HttpClient\Bean;


use EasySwoole\Spl\SplBean;

class Url extends SplBean
{
protected $scheme = 'http';
protected $host;
protected $port;
protected $path;
protected $query;

/**
* @return mixed
*/
public function getScheme()
{
return $this->scheme;
}

/**
* @param mixed $scheme
*/
public function setScheme($scheme): void
{
$this->scheme = $scheme;
}

/**
* @return mixed
*/
public function getHost()
{
return $this->host;
}

/**
* @param mixed $host
*/
public function setHost($host): void
{
$this->host = $host;
}

/**
* @return mixed
*/
public function getPort()
{
return $this->port;
}

/**
* @param mixed $port
*/
public function setPort($port): void
{
$this->port = $port;
}

/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}

/**
* @param mixed $path
*/
public function setPath($path): void
{
$this->path = $path;
}

/**
* @return mixed
*/
public function getQuery()
{
return $this->query;
}

/**
* @param mixed $query
*/
public function setQuery($query): void
{
$this->query = $query;
}

}
15 changes: 15 additions & 0 deletions src/Exception/InvalidUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/12/25
* Time: 11:02 AM
*/

namespace EasySwoole\HttpClient\Exception;


class InvalidUrl extends \Exception
{

}
180 changes: 180 additions & 0 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/12/25
* Time: 10:43 AM
*/

namespace EasySwoole\HttpClient;


use EasySwoole\HttpClient\Bean\Url;
use EasySwoole\HttpClient\Exception\InvalidUrl;
use Swoole\Coroutine\Http\Client;

class HttpClient
{
protected $url;
protected $header = [
"User-Agent" => 'EasySwooleHttpClient/0.1',
'Accept' => 'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding' => 'gzip',
'Pragma' => 'no-cache',
'Cache-Control' => 'no-cache'
];
protected $clientSetting = [];
protected $swooleHttpClient;
protected $postData;
protected $isPost = false;
/*
* addFile 方法
*/
protected $postFiles = [];
/*
* addData 方法
*/
protected $postDataByAddData = [];
function __construct(?string $url = null)
{
if(!empty($url)){
$this->setUrl($url);
}
$this->setTimeout(0.1);
}

function setUrl(string $url):HttpClient
{
$info = parse_url($url);
$this->url = new Url($info);
if(empty($this->url->getHost())){
throw new InvalidUrl("url: {$url} invalid");
}
return $this;
}

public function setTimeout(float $connectTimeout):HttpClient
{
$this->clientSetting['timeout'] = $connectTimeout;
return $this;
}

public function setClientSettings(array $settings):HttpClient
{
$this->clientSetting = $settings;
return $this;
}

public function setClientSetting($key,$setting):HttpClient
{
$this->clientSetting[$key] = $setting;
return $this;
}

public function setHeaders(array $header):HttpClient
{
$this->header = $header;
return $this;
}

public function setHeader($key,$value):HttpClient
{
$this->header[$key] = $value;
return $this;
}

public function getSwooleHttpClient():?Client
{
return $this->swooleHttpClient;
}

public function post($data = null)
{
$this->postData = $data;
$this->isPost = true;
}

public function postJSON(string $json):HttpClient
{
$this->setHeader('Content-Type','text/json');
$this->post($json);
return $this;
}

public function postXML(string $xml):HttpClient
{
$this->setHeader('Content-Type','text/xml');
$this->post($xml);
return $this;
}

public function addFile(string $path, string $name, string $mimeType = null, string $filename = null, int $offset = 0, int $length = 0):HttpClient
{
$this->isPost = true;
$this->postFiles[] = [
$path,$name,$mimeType,$filename,$offset,$length
];
return $this;
}

public function addData(string $data, string $name, string $mimeType = null, string $filename = null):HttpClient
{
$this->isPost = true;
$this->postDataByAddData[] = [
$data,$name,$mimeType,$filename
];
return $this;
}

public function exec(?float $timeout = null)
{
if($timeout !== null){
$this->setTimeout($timeout);
}
$client = $this->createClient();
if($this->isPost){
foreach ($this->postFiles as $file){
$client->addFile(...$file);
}
foreach ($this->postDataByAddData as $addDatum){
$client->addData(...$addDatum);
}
$client->post($this->url->getPath(),$this->url->getQuery(), $this->postData);
}else{
$client->get($this->getUri($this->url->getPath(),$this->url->getQuery()));
}

$body = $client->body;
var_dump($body);
$client->close();

}

private function createClient():Client
{
if($this->url instanceof Url){
$port = $this->url->getPort();
$ssl = false;
if(empty($port)){
if($this->url->getScheme() == 'https'){
$port = 443;
$ssl = true;
}else{
$port = 80;
}
}
$cli = new Client($this->url->getHost(), $port, $ssl);
$cli->set($this->clientSetting);
$cli->setHeaders($this->header);
$this->swooleHttpClient = $cli;
return $this->swooleHttpClient;
}else{
throw new InvalidUrl("url is empty");
}
}

private function getUri(string $path, string $query): string
{
return !empty($query) ? $path . '?' . $query : $path;
}
}

0 comments on commit 6b9ec00

Please sign in to comment.