2017 © Pedro Peláez
 

library g-http

Simple Http client base on GuzzleHttp

image

jaeger/g-http

Simple Http client base on GuzzleHttp

  • Tuesday, December 26, 2017
  • by jae
  • Repository
  • 1 Watchers
  • 0 Stars
  • 13,354 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 36 % Grown

The README.md

GHttp

基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp, (*1)

安装

composer require jaeger/g-http

用法

1. get / getJson


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

$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

GHttp::download('http://sw.bos.baidu.com/setup.exe','./path/to/xx.exe');

4. concurrent requests

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();
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 Symfony-Cache: https://symfony.com/doc/current/components/cache.html, (*2)

  • Use filesystem cache
use Jaeger\GHttp;

$rt = GHttp::get('http://httpbin.org/get',[
    'wd' => 'QueryList'
],[
    'cache' => __DIR__,
    'cache_ttl' => 120 //seconds
]);

  • Use predis cache

Install predis:, (*3)

composer require predis/predis

Usage:, (*4)

use Jaeger\GHttp;
use Symfony\Component\Cache\Adapter\RedisAdapter;

$cache = new RedisAdapter(

    // the object that stores a valid connection to your Redis system
    $redis = RedisAdapter::createConnection(
        'redis://localhost'
    ),

    // the string prefixed to the keys of the items stored in this cache
    $namespace = 'cache',

    // the default lifetime (in seconds) for cache items that do not define their
    // own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
    // until RedisAdapter::clear() is invoked or the server(s) are purged)
    $defaultLifetime = 0
);

$rt = GHttp::get('http://httpbin.org/get',[
    'wd' => 'QueryList'
],[
    'cache' => $cache,
    'cache_ttl' => 120
]);

print_r($rt);

The Versions

26/12 2017

dev-master

9999999-dev

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger

26/12 2017

V1.4.1

1.4.1.0

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger

19/12 2017

V1.4

1.4.0.0

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger

25/09 2017

V1.3

1.3.0.0

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger

25/09 2017

V1.2

1.2.0.0

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger

13/06 2017

V1.1

1.1.0.0

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger

09/06 2017

V1.0

1.0.0.0

Simple Http client base on GuzzleHttp

  Sources   Download

MIT

The Requires

 

by Jaeger