中秋节快乐
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
.idea
|
||||
/vendor/
|
||||
composer.lock
|
||||
Vendored
+158
@@ -0,0 +1,158 @@
|
||||
# GHttp
|
||||
基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp
|
||||
|
||||
## 安装
|
||||
|
||||
```
|
||||
composer require jaeger/g-http
|
||||
```
|
||||
|
||||
## 用法
|
||||
|
||||
#### 1. get / getJson
|
||||
```php
|
||||
|
||||
use Jaeger\GHttp;
|
||||
|
||||
$rt = GHttp::get('https://www.baidu.com/s?wd=QueryList');
|
||||
|
||||
$rt = GHttp::get('https://www.baidu.com/s','wd=QueryList&wd2=teststr');
|
||||
|
||||
//or
|
||||
|
||||
$rt = GHttp::get('https://www.baidu.com/s',[
|
||||
'wd' => 'QueryList',
|
||||
'wd2' => 'teststr'
|
||||
]);
|
||||
|
||||
//opt
|
||||
|
||||
$rt = GHttp::get('https://www.baidu.com/s',[
|
||||
'wd' => 'QueryList'
|
||||
],[
|
||||
'headers' => [
|
||||
'referer' => 'https://baidu.com',
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NTChrome/58.0.3029.110 Safari/537.36',
|
||||
'Cookie' => 'cookie xxx'
|
||||
]
|
||||
]);
|
||||
|
||||
$rt = GHttp::getJson('https://xxxx.com/json');
|
||||
|
||||
```
|
||||
|
||||
#### 2.post / postRaw / postJson
|
||||
```php
|
||||
$rt = GHttp::post('https://www.posttestserver.com/post.php',[
|
||||
'name' => 'QueryList',
|
||||
'password' => 'ql'
|
||||
]);
|
||||
|
||||
$rt = GHttp::post('https://www.posttestserver.com/post.php','name=QueryList&password=ql');
|
||||
|
||||
|
||||
$rt = GHttp::postRaw('http://httpbin.org/post','raw data');
|
||||
$rt = GHttp::postRaw('http://httpbin.org/post',['aa' => 11,'bb' => 22]);
|
||||
|
||||
|
||||
$rt = GHttp::postJson('http://httpbin.org/post',['aa' => 11,'bb' => 22]);
|
||||
$rt = GHttp::postJson('http://httpbin.org/post','aa=11&bb=22');
|
||||
|
||||
```
|
||||
#### 3.download
|
||||
|
||||
```php
|
||||
GHttp::download('http://sw.bos.baidu.com/setup.exe','./path/to/xx.exe');
|
||||
```
|
||||
### 4. concurrent requests
|
||||
```php
|
||||
use Jaeger\GHttp;
|
||||
|
||||
$urls = [
|
||||
'http://httpbin.org/get?name=php',
|
||||
'http://httpbin.org/get?name=go',
|
||||
'http://httpbin.org/get?name=c#',
|
||||
'http://httpbin.org/get?name=java'
|
||||
];
|
||||
|
||||
GHttp::multiRequest($urls)->withHeaders([
|
||||
'X-Powered-By' => 'Jaeger'
|
||||
])->withOptions([
|
||||
'timeout' => 10
|
||||
])->concurrency(2)->success(function($response,$index){
|
||||
print_r((String)$response->getBody());
|
||||
print_r($index);
|
||||
})->error(function($reason,$index){
|
||||
print_r($reason);
|
||||
})->get();
|
||||
```
|
||||
|
||||
```php
|
||||
use Jaeger\GHttp;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
$requests = [
|
||||
new Request('POST','http://httpbin.org/post',[
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'User-Agent' => 'g-http'
|
||||
],http_build_query([
|
||||
'name' => 'php'
|
||||
])),
|
||||
new Request('POST','http://httpbin.org/post',[
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'User-Agent' => 'g-http'
|
||||
],http_build_query([
|
||||
'name' => 'go'
|
||||
])),
|
||||
new Request('POST','http://httpbin.org/post',[
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'User-Agent' => 'g-http'
|
||||
],http_build_query([
|
||||
'name' => 'c#'
|
||||
]))
|
||||
];
|
||||
|
||||
GHttp::multiRequest($requests)->success(function($response,$index){
|
||||
print_r((String)$response->getBody());
|
||||
print_r($index);
|
||||
})->post();
|
||||
```
|
||||
### 5. Request with cache
|
||||
|
||||
Base on PHP-Cache: http://www.php-cache.com
|
||||
|
||||
- Use filesystem cache
|
||||
```php
|
||||
use Jaeger\GHttp;
|
||||
|
||||
$rt = GHttp::get('http://httpbin.org/get',[
|
||||
'wd' => 'QueryList'
|
||||
],[
|
||||
'cache' => __DIR__,
|
||||
'cache_ttl' => 120 //seconds
|
||||
]);
|
||||
|
||||
```
|
||||
|
||||
- Use predis cache
|
||||
|
||||
Install predis adapter:
|
||||
```
|
||||
composer require cache/predis-adapter
|
||||
```
|
||||
|
||||
Usage:
|
||||
```php
|
||||
use Jaeger\GHttp;
|
||||
use Cache\Adapter\Predis\PredisCachePool;
|
||||
|
||||
$client = new \Predis\Client('tcp:/127.0.0.1:6379');
|
||||
$pool = new PredisCachePool($client);
|
||||
|
||||
$rt = GHttp::get('http://httpbin.org/get',[
|
||||
'wd' => 'QueryList'
|
||||
],[
|
||||
'cache' => $pool,
|
||||
'cache_ttl' => 120 //seconds
|
||||
]);
|
||||
```
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "jaeger/g-http",
|
||||
"description": "Simple Http client base on GuzzleHttp",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jaeger",
|
||||
"email": "JaegerCode@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"cache/filesystem-adapter": "^1",
|
||||
"guzzlehttp/guzzle": "^6.0 || ^7.0"
|
||||
},
|
||||
"autoload":{
|
||||
"psr-4":{
|
||||
"Jaeger\\":"src"
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/10
|
||||
* Time: 下午4:04
|
||||
*/
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
use Jaeger\GHttp;
|
||||
|
||||
$urls = [
|
||||
'http://httpbin.org/get?name=php',
|
||||
'http://httpbin.org/get?name=go',
|
||||
'http://httpbin.org/get?name=c#',
|
||||
'http://httpbin.org/get?name=java'
|
||||
];
|
||||
|
||||
GHttp::multiRequest($urls)->withHeaders([
|
||||
'X-Powered-By' => 'Jaeger'
|
||||
])->withOptions([
|
||||
'timeout' => 10
|
||||
])->concurrency(2)->success(function($response,$index){
|
||||
print_r((String)$response->getBody());
|
||||
print_r($index);
|
||||
})->error(function($reason,$index){
|
||||
print_r($reason);
|
||||
})->get();
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/10
|
||||
* Time: 下午6:51
|
||||
*/
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
use Jaeger\GHttp;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
$requests = [
|
||||
new Request('POST','http://httpbin.org/post',[
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'User-Agent' => 'g-http'
|
||||
],http_build_query([
|
||||
'name' => 'php'
|
||||
])),
|
||||
new Request('POST','http://httpbin.org/post',[
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'User-Agent' => 'g-http'
|
||||
],http_build_query([
|
||||
'name' => 'go'
|
||||
])),
|
||||
new Request('POST','http://httpbin.org/post',[
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'User-Agent' => 'g-http'
|
||||
],http_build_query([
|
||||
'name' => 'c#'
|
||||
]))
|
||||
];
|
||||
|
||||
GHttp::multiRequest($requests)->success(function($response,$index){
|
||||
print_r((String)$response->getBody());
|
||||
print_r($index);
|
||||
})->post();
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/11
|
||||
* Time: 下午6:48
|
||||
*/
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
use Jaeger\GHttp;
|
||||
use Cache\Adapter\Predis\PredisCachePool;
|
||||
|
||||
|
||||
$rt = GHttp::get('http://httpbin.org/get',[
|
||||
'wd' => 'QueryList'
|
||||
],[
|
||||
'cache' => __DIR__,
|
||||
'cache_ttl' => 120
|
||||
]);
|
||||
|
||||
print_r($rt);
|
||||
|
||||
$client = new \Predis\Client('tcp:/127.0.0.1:6379');
|
||||
$pool = new PredisCachePool($client);
|
||||
|
||||
$rt = GHttp::get('http://httpbin.org/get',[
|
||||
'wd' => 'QueryList'
|
||||
],[
|
||||
'cache' => $pool,
|
||||
'cache_ttl' => 120
|
||||
]);
|
||||
|
||||
print_r($rt);
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/11
|
||||
* Time: 下午6:48
|
||||
*/
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
use Jaeger\GHttp;
|
||||
|
||||
$rt = GHttp::get('http://httpbin.org/get',[
|
||||
'wd' => 'QueryList'
|
||||
],[
|
||||
'headers' => [
|
||||
'referer' => 'https://baidu.com',
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NTChrome/58.0.3029.110 Safari/537.36',
|
||||
'Cookie' => 'cookie xxx'
|
||||
],
|
||||
]);
|
||||
|
||||
print_r($rt);
|
||||
Vendored
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/11
|
||||
* Time: 下午6:39
|
||||
*/
|
||||
|
||||
namespace Jaeger;
|
||||
|
||||
|
||||
use Cache\Adapter\Common\AbstractCachePool;
|
||||
use Cache\Adapter\Filesystem\FilesystemCachePool;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
|
||||
class Cache extends GHttp
|
||||
{
|
||||
public static function remember($name, $arguments)
|
||||
{
|
||||
$cachePool = null;
|
||||
$cacheConfig = self::initCacheConfig($arguments);
|
||||
|
||||
if (empty($cacheConfig['cache'])) {
|
||||
return self::$name(...$arguments);
|
||||
}
|
||||
if (is_string($cacheConfig['cache'])) {
|
||||
$filesystemAdapter = new Local($cacheConfig['cache']);
|
||||
$filesystem = new Filesystem($filesystemAdapter);
|
||||
$cachePool = new FilesystemCachePool($filesystem);
|
||||
}else if ($cacheConfig['cache'] instanceof AbstractCachePool) {
|
||||
$cachePool = $cacheConfig['cache'];
|
||||
}
|
||||
|
||||
$cacheKey = self::getCacheKey($name,$arguments);
|
||||
$data = $cachePool->get($cacheKey);
|
||||
if(empty($data)) {
|
||||
$data = self::$name(...$arguments);
|
||||
if(!empty($data)) {
|
||||
$cachePool->set($cacheKey,$data,$cacheConfig['cache_ttl']);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected static function initCacheConfig($arguments)
|
||||
{
|
||||
$cacheConfig = [
|
||||
'cache' => null,
|
||||
'cache_ttl' => null
|
||||
];
|
||||
if(!empty($arguments[2])) {
|
||||
$cacheConfig = array_merge([
|
||||
'cache' => null,
|
||||
'cache_ttl' => null
|
||||
],$arguments[2]);
|
||||
}
|
||||
return $cacheConfig;
|
||||
}
|
||||
|
||||
protected static function getCacheKey($name, $arguments)
|
||||
{
|
||||
return md5($name.'_'.json_encode($arguments));
|
||||
}
|
||||
}
|
||||
Vendored
+164
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp
|
||||
*
|
||||
* @Author: Jaeger <JaegerCode@gmail.com>
|
||||
*
|
||||
* @Version V1.0
|
||||
*/
|
||||
|
||||
namespace Jaeger;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/**
|
||||
* Class GHttp
|
||||
* @package Jaeger
|
||||
*
|
||||
* @method static string get($url,$args = null,$otherArgs = [])
|
||||
* @method static mixed getJson($url, $args = null, $otherArgs = [])
|
||||
* @method static string post($url,$args = null,$otherArgs = [])
|
||||
* @method static string postRaw($url, $raw = null, $otherArgs = [])
|
||||
* @method static string postJson($url, $args = null, $otherArgs = [])
|
||||
*/
|
||||
class GHttp
|
||||
{
|
||||
private static $client = null;
|
||||
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
$protectedName = '_'.$name;
|
||||
if(method_exists(self::class,$protectedName)){
|
||||
return Cache::remember($protectedName, $arguments);
|
||||
}
|
||||
throw new MethodNotFoundException('Call undefined method '.self::class.':'.$name.'()');
|
||||
}
|
||||
|
||||
public static function getClient(array $config = [])
|
||||
{
|
||||
if(self::$client == null){
|
||||
self::$client = new Client($config);
|
||||
}
|
||||
return self::$client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $args
|
||||
* @param array $otherArgs
|
||||
* @return string
|
||||
*/
|
||||
protected static function _get($url,$args = null,$otherArgs = [])
|
||||
{
|
||||
is_string($args) && parse_str($args,$args);
|
||||
$args = array_merge([
|
||||
'verify' => false,
|
||||
'query' => $args,
|
||||
'headers' => [
|
||||
'referer' => $url,
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
|
||||
]
|
||||
],$otherArgs);
|
||||
$client = self::getClient();
|
||||
$response = $client->request('GET', $url,$args);
|
||||
return (string)$response->getBody();
|
||||
}
|
||||
|
||||
protected static function _getJson($url, $args = null, $otherArgs = [])
|
||||
{
|
||||
$data = self::get($url, $args , $otherArgs);
|
||||
return json_decode($data,JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $args
|
||||
* @param array $otherArgs
|
||||
* @return string
|
||||
*/
|
||||
protected static function _post($url,$args = null,$otherArgs = [])
|
||||
{
|
||||
is_string($args) && parse_str($args,$args);
|
||||
$args = array_merge([
|
||||
'verify' => false,
|
||||
'form_params' => $args,
|
||||
'headers' => [
|
||||
'referer' => $url,
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
|
||||
]
|
||||
],$otherArgs);
|
||||
$client = self::getClient();
|
||||
$response = $client->request('Post', $url,$args);
|
||||
return (string)$response->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param null $raw
|
||||
* @param array $otherArgs
|
||||
* @return string
|
||||
*/
|
||||
protected static function _postRaw($url, $raw = null, $otherArgs = [])
|
||||
{
|
||||
is_array($raw) && $raw = json_encode($raw);
|
||||
$args = array_merge([
|
||||
'verify' => false,
|
||||
'body' => $raw,
|
||||
'headers' => [
|
||||
'referer' => $url,
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
|
||||
]
|
||||
],$otherArgs);
|
||||
$client = self::getClient();
|
||||
$response = $client->request('Post', $url,$args);
|
||||
return (string)$response->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param null $args
|
||||
* @param array $otherArgs
|
||||
* @return string
|
||||
*/
|
||||
protected static function _postJson($url, $args = null, $otherArgs = [])
|
||||
{
|
||||
is_string($args) && parse_str($args,$args);
|
||||
$args = array_merge([
|
||||
'verify' => false,
|
||||
'json' => $args,
|
||||
'headers' => [
|
||||
'referer' => $url,
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
|
||||
]
|
||||
],$otherArgs);
|
||||
$client = self::getClient();
|
||||
$response = $client->request('Post', $url,$args);
|
||||
return (string)$response->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $filePath
|
||||
* @param null $args
|
||||
* @param array $otherArgs
|
||||
* @return string
|
||||
*/
|
||||
public static function download($url,$filePath,$args = null,$otherArgs = [])
|
||||
{
|
||||
$otherArgs = array_merge($otherArgs,[
|
||||
'sink' => $filePath,
|
||||
]);
|
||||
return self::get($url,$args,$otherArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $urls
|
||||
* @return MultiRequest
|
||||
*/
|
||||
public static function multiRequest($urls)
|
||||
{
|
||||
$client = self::getClient();
|
||||
return MultiRequest::newRequest($client)->urls($urls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/12
|
||||
* Time: 上午10:56
|
||||
*/
|
||||
|
||||
namespace Jaeger;
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class MethodNotFoundException extends Exception
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Jaeger <JaegerCode@gmail.com>
|
||||
* Date: 18/12/10
|
||||
* Time: 下午6:04
|
||||
*/
|
||||
|
||||
namespace Jaeger;
|
||||
use GuzzleHttp\Client;
|
||||
use Closure;
|
||||
use GuzzleHttp\Pool;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
class MultiRequest
|
||||
{
|
||||
protected $client;
|
||||
protected $headers = [];
|
||||
protected $options = [];
|
||||
protected $successCallback;
|
||||
protected $errorCallback;
|
||||
protected $urls = [];
|
||||
protected $method;
|
||||
protected $concurrency = 5;
|
||||
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->headers = [
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
|
||||
];
|
||||
}
|
||||
|
||||
public static function newRequest(Client $client)
|
||||
{
|
||||
$request = new self($client);
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function withHeaders($headers)
|
||||
{
|
||||
$this->headers = array_merge($this->headers,$headers);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withOptions($options)
|
||||
{
|
||||
$this->options = $options;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function concurrency($concurrency)
|
||||
{
|
||||
$this->concurrency = $concurrency;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function success(Closure $success)
|
||||
{
|
||||
$this->successCallback = $success;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function error(Closure $error)
|
||||
{
|
||||
$this->errorCallback = $error;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function urls(array $urls)
|
||||
{
|
||||
$this->urls = $urls;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
$this->method = 'GET';
|
||||
$this->send();
|
||||
}
|
||||
|
||||
public function post()
|
||||
{
|
||||
$this->method = 'POST';
|
||||
$this->send();
|
||||
}
|
||||
|
||||
protected function send()
|
||||
{
|
||||
$client = $this->client;
|
||||
|
||||
$requests = function ($urls) use($client){
|
||||
foreach ($urls as $url) {
|
||||
if (is_string($url)) {
|
||||
yield new Request($this->method,$url,$this->headers);
|
||||
} else {
|
||||
yield $url;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$pool = new Pool($client, $requests($this->urls), [
|
||||
'concurrency' => $this->concurrency,
|
||||
'fulfilled' => $this->successCallback,
|
||||
'rejected' => $this->errorCallback,
|
||||
'options' => $this->options
|
||||
]);
|
||||
|
||||
$promise = $pool->promise();
|
||||
$promise->wait();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user