findologic/curl-bundle

该包已被弃用且不再维护。未建议替代包。

允许您轻松创建和执行 HTTP 请求

安装: 92

依赖者: 1

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 12

类型:symfony-bundle

v1.2 2012-03-23 03:47 UTC

This package is auto-updated.

Last update: 2022-02-01 12:40:06 UTC


README

RemoteHttpKernel 提供了作为 Symfony 标准 HttpKernel 的替代方案。而不是使用本地应用程序,它通过 cURL 处理请求对象,并将结果解析为合适的响应对象。

Symfony 安装

首先,检出代码副本。只需将以下内容添加到您的 Symfony Standard Distribution 的 deps 文件中

[ZeroemCurlBundle]
    git=git://github.com/zeroem/ZeroemCurlBundle.git
    target=/bundles/Zeroem/CurlBundle/

然后,将包添加到您的 AppKernel 并使用自动加载器注册命名空间

    // app/AppKernel.php
    $bundles = array(
        // ...
        new Zeroem\CurlBundle\ZeroemCurlBundle(),        
        // ...
    );
    // app/autoload.php
    $loader->registerNamespaces(array(
        // ...
        'Zeroem'              => __DIR__.'/../vendor/bundles'
        // ...
    ));

现在使用 vendors 脚本来将新添加的仓库克隆到您的项目中

    php bin/vendors install

Composer 安装

zeroem/curl-bundle 添加到您的 composer.json 文件中

{
   ...
   "require": {
   ...
       "zeroem/curl-bundle": ""
   ...
   }
...
}

并包含 composer 自动加载器

require("./vendor/.composer/autoload.php");

特性

RemoteHttpKernel

RemoteHttpKernel 为 Symfony 使用的标准请求/响应架构与 cURL 库之间提供桥梁。

use Symfony\Component\HttpFoundation\Request;
use Zeroem\CurlBundle\HttpKernel\RemoteHttpKernel;

$request = Request::create("http://www.symfony.com");
$remoteKernel = new RemoteHttpKernel();
$response = $remoteKernel->handle($request);

注意事项

由于 HttpFoundation\Request 对象生成请求 Uri 的方式,对 Request::$query 所做的任何更改在执行 cURL 请求时都不会反映出来。我目前正在寻找解决此问题的正确方法。

RequestGenerator

RequestGenerator 简化了构建多个类似 cURL 请求对象。

use Zeroem\CurlBundle\Curl\RequestGenerator;

$generator = new RequestGenerator(array(CURLOPT_RETURNTRANSFER=>true));

// Automatically has CURLOPT_RETURNTRANSFER set to true
$request = $generator->getRequest();

目标

提供一种干净的对象化解决方案,用于与远程 HTTP 服务交互,无需了解所有各种 cURL API 选项,也不需要创建自己的 HTTP 请求/响应架构。

动机

我最初在寻找一个提供面向对象接口的 cURL_* 函数的 Symfony Bundle。在这个过程中,我意识到我实际上并不需要一个 cURL 包装器,我需要一个能够执行 HTTP 请求并以有意义的方式返回结果的东西。因此,我决定使用现有的 Symfony 组件并构建一个定制的 HttpKernel,将 cURL 作为一个实现细节而非项目目标。