沙里拉/卷曲

Laravel 5 的 PHP cURL 库

dev-master 2016-05-30 18:07 UTC

This package is auto-updated.

Last update: 2024-09-25 18:54:25 UTC


README

Laravel 5 框架的定制 PHP cURL 库

安装

通过 Composer 拉取此包。

    {
        "require": {
            "sargilla/curl": "dev-master"
        }
    }

Laravel 5.* 集成

将服务提供者添加到您的 config/app.php 文件中

    'providers'     => array(

        //...
        Sargilla\Curl\CurlServiceProvider::class,

    ),

将别名添加到您的 config/app.php 文件中

    'aliases'       => array(

        //...
        'Curl'          => Sargilla\Curl\Facades\Curl::class,

    ),

Laravel 之外集成

在您希望使用此包的地方创建一个新的 CurlService 实例

    $curlService = new \Sargilla\Curl\CurlService();

用法

Laravel 用法

该包提供了一个易于使用界面,用于从您的应用程序发送 cURL 请求。它提供了一个类似于 Laravel 查询构建器的流畅接口,以便轻松配置请求。有几个实用方法允许您轻松地添加某些选项到请求中。

发送 GET 请求

为了发送一个 GET 请求,您需要使用由包提供的 get() 方法

    // Send a GET request to: http://www.foo.com/bar
    

    // Send a GET request to: http://www.foo.com/bar?foz=baz
    
    // Send a GET request to: http://www.foo.com/bar?foz=baz using JSON
    

发送 POST 请求

POST 请求与 GET 请求类似,但使用 post() 方法代替

    // Send a POST request to: http://www.foo.com/bar
    
    // Send a POST request to: http://www.foo.com/bar
    
    // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON
    
    // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON and return as associative array
    

下载文件

为了下载文件,您可以使用 download() 方法

    // Download an image from: file http://www.foo.com/bar.png
   

Laravel 之外用法

在 Laravel 之外的使用与之前描述的使用相同。唯一的区别是您将无法使用外观来访问 CurlService

    $curlService = new \Sargilla\Curl\CurlService();

    // Send a GET request to: http://www.foo.com/bar
    $response = $curlService->to('http://www.foo.com/bar')
        ->get();
        
    // Send a POST request to: http://www.foo.com/bar
    $response = $curlService->to('http://www.foo.com/bar')
        ->post();

许可证

此模板是开源软件,采用MIT 许可证

联系方式

圣地亚哥·阿吉拉(开发者)