evaisse/simple-http-bundle

基于httpfoundation组件(而非guzzle)构建的一个非常简单的symfony http客户端bundle,使用cURL和cURL多线程。

v3.4.1 2023-11-30 10:27 UTC

README

虽然仍然在维护,但我建议您用symfony/http-client替换此bundle,它有更好的支持和方便的插件支持。

基于httpfoundation组件(而非guzzle)构建的symfony2/3/4/5 http客户端bundle,使用cURL作为引擎。

Coverage Status

快速入门

获取简单API

$http = $this->container->get('http');
$http = $this->get('http');

// same as  

try {
    $http->GET('http://httpbin.org/ip'); // yeah, that's all.
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
    // handle errors
}

try {
    $data = $http->POST('http://httpbin.org/post', $myArgs);
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
    // handle errors
}

使用替换语法轻松路由(类似于symfony2/laravel等php框架)

$data = $http->GET('http://httpbin.org/status/{code}', array(
    "code" => 200
));
// will call http://httpbin.org/status/200

$data = $http->GET('http://httpbin.org/status/{code}', array(
    "code" => 200,
    "foo" => "bar" 
));
// will call http://httpbin.org/status/200?foo=bar

开发特性

简单的http bundle提供您一个漂亮的探查器工具栏附加组件

  • 请求/响应体和头部详细信息
  • 方便的Cookies面板
  • 外部调试链接(使用X-Debug-Link头部访问远程探查器)
  • 请求时间在时间轴面板中显示

重放功能

将其添加到routing.yml文件中

_simple_http:
    resource: "@SimpleHttpBundle/Controller/"
    type:     annotation
    prefix:   /_profiler/simple-http

或者

_simple_http:
    resource: '@SimpleHttpBundle/Resources/config/routing.yml'

您现在可以直接从探查器调试面板重新发送HTTP调用

面板详细信息

![面板详细信息] (https://raw.githubusercontent.com/evaisse/SimpleHttpBundle/master/Resources/doc/profiler-panels.png)

工具栏块

![工具栏块] (https://raw.githubusercontent.com/evaisse/SimpleHttpBundle/master/Resources/doc/profiler-toolbar.png)

时间轴面板

![时间轴面板] (https://raw.githubusercontent.com/evaisse/SimpleHttpBundle/master/Resources/doc/profiler-timeline.png)

完整API

使用事务工厂的完整API

$transac = $http->prepare('GET', 'http://httpbin.org/ip');

$transac->execute();

并行执行

$a = $http->prepare('GET', 'http://httpbin.org/ip');
$b = $http->prepare('PUT', 'http://httpbin.org/put');
$c = $http->prepare('POST', 'http://httpbin.org/post');

$http->execute([
    $a, 
    $b,
    $c
]);

$a->hasError() || $a->getResult();
$b->hasError() || $b->getResult();
$c->hasError() || $c->getResult();

JSON服务

print $http->prepare('POST', 'http://httpbin.org/ip', $_SERVER)
           ->json()
           ->execute()
           ->getResult()['ip'];

文件上传

$http->prepare('PUT', 'http://httpbin.org/put')
     ->addFile('f1', './myfile.txt')
     ->addFile('f2', './myfile.txt')
     ->execute();


$http->prepare('POST', 'http://httpbin.org/post', [
        'infos' => 'foo',
        'bar'   => 'so so',
    ])
     ->addFile('f1', './myfile.txt')
     ->addFile('f2', './myfile.txt')
     ->execute();

Cookies持久化

$a = $http->prepare('GET',  'http://httpbin.org/ip');
$b = $http->prepare('PUT',  'http://httpbin.org/put');
$c = $http->prepare('POST', 'http://httpbin.org/post');


$cookies = $http->getDefaultCookieJar();
 // $cookies = $http->getCookieJar($session); if you want to directly store in user session

$http->execute([
    $a, 
    $b,
    $c
], $cookies);

dump($cookies);

Promise使用

$stmt = $http->prepare('PUT', 'http://httpbin.org/put')

$stmt->onSuccess(function ($data) {
    // handle data
})->onError(function (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
    // handle errors
})->onFinish(function () {
    // like "finally"
});

$http->execute([
    $stmt
]);