为Laravel框架定制的PHP cURL库 - 由LaravelMasterPackage开发
Requires
- php: >=5.4.0
- ext-curl: *
- illuminate/support: >=4.0
This package is auto-updated.
Last update: 2024-09-04 12:41:59 UTC
README
为Laravel框架定制的PHP cURL库 - 由LaravelMasterPackage开发。
此包提供了一个易于使用的接口,用于从您的PHP Web应用程序发送cURL请求。该包提供了一个直观的流畅接口,类似于Laravel查询构建器,可以轻松配置请求。此外,还有一些实用方法,允许您轻松地向请求添加某些选项。这使得创建和使用cURL请求变得更容易,同时也使您的代码更易于理解。
提供的功能完全独立于框架,但也包含一个Laravel服务提供者,以便轻松集成到您的Laravel项目中。
发布问题前注意事项:在发布有关此包的问题时,请务必提供尽可能多的关于请求的信息。这包括您尝试将其转换为包语法的示例cURL请求、您的实际包语法(完整的请求)以及(如果可能)一个我可以用来测试请求的示例URL。
安装
通过Composer拉取此包。
{ "require": { "laravelmasterpackage/curl": "6.*" } }
或者在终端中运行:composer require laravelmasterpackage/curl
Laravel 5.5+ 集成
Laravel的包发现将为您处理集成。
Laravel 5.* 集成
将服务提供者添加到您的 config/app.php 文件中
'providers' => array( //... LaravelMasterPackage\Curl\CurlServiceProvider::class, ),
将外观添加到您的 config/app.php 文件中
'aliases' => array( //... 'Curl' => LaravelMasterPackage\Curl\Facades\Curl::class, ),
Laravel 4.* 集成
将服务提供者添加到您的 app/config/app.php 文件中
'providers' => array( //... 'LaravelMasterPackage\Curl\CurlServiceProvider', ),
将外观添加到您的 app/config/app.php 文件中
'facades' => array( //... 'Curl' => 'LaravelMasterPackage\Curl\Facades\Curl', ),
Lumen 5.* 集成
在您的 bootstrap/app.php 中,确保您已取消注释以下行(大约在26行附近)
$app->withFacades();
然后,注册您的类别名
class_alias('LaravelMasterPackage\Curl\Facades\Curl', 'Curl');
最后,您必须注册您的ServiceProvider(大约在70-80行附近)
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
// $app->register('App\Providers\AppServiceProvider');
// Package service providers
$app->register(LaravelMasterPackage\Curl\CurlServiceProvider::class);
不使用Laravel的集成
在您想使用此包的地方创建一个新的 CurlService 实例
$curlService = new \LaravelMasterPackage\Curl\CurlService();
用法
Laravel用法
此包提供了一个易于使用的接口,用于从您的应用程序发送cURL请求。该包提供了一个类似于Laravel查询构建器的流畅接口,可以轻松配置请求。有几个实用方法允许您轻松地向请求添加某些选项。如果没有适用的实用方法,您也可以使用通用的 withOption 方法。
发送GET请求
为了发送一个 GET 请求,您需要使用由包提供的 get() 方法
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar $response = Curl::to('http://www.foo.com/bar') ->get(); // Send a GET request to: http://www.foo.com/bar?foz=baz $response = Curl::to('http://www.foo.com/bar') ->withData( array( 'foz' => 'baz' ) ) ->get(); // Send a GET request to: http://www.foo.com/bar?foz=baz using JSON $response = Curl::to('http://www.foo.com/bar') ->withData( array( 'foz' => 'baz' ) ) ->asJson() ->get();
发送POST请求
POST请求与 GET 请求类似,但使用 post() 方法代替
use LaravelMasterPackage\Curl\Facades\Curl; // Send a POST request to: http://www.foo.com/bar $response = Curl::to('http://www.foo.com/bar') ->post(); // Send a POST request to: http://www.foo.com/bar $response = Curl::to('http://www.foo.com/bar') ->withData( array( 'foz' => 'baz' ) ) ->post(); // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON $response = Curl::to('http://www.foo.com/bar') ->withData( array( 'foz' => 'baz' ) ) ->asJson() ->post(); // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON and return as associative array $response = Curl::to('http://www.foo.com/bar') ->withData( array( 'foz' => 'baz' ) ) ->asJson( true ) ->post();
发送PUT请求
PUT请求与 POST 请求类似,但使用 put() 方法代替
use LaravelMasterPackage\Curl\Facades\Curl; // Send a PUT request to: http://www.foo.com/bar/1 with arguments 'foz' = 'baz' using JSON $response = Curl::to('http://www.foo.com/bar/1') ->withData( array( 'foz' => 'baz' ) ) ->asJson() ->put();
发送PATCH请求
PATCH请求与 POST 请求类似,但使用 patch() 方法代替
use LaravelMasterPackage\Curl\Facades\Curl; // Send a PATCH request to: http://www.foo.com/bar/1 with arguments 'foz' = 'baz' using JSON $response = Curl::to('http://www.foo.com/bar/1') ->withData( array( 'foz' => 'baz' ) ) ->asJson() ->patch();
发送DELETE请求
DELETE请求与 GET 请求类似,但使用 delete() 方法代替
use LaravelMasterPackage\Curl\Facades\Curl; // Send a DELETE request to: http://www.foo.com/bar/1 using JSON $response = Curl::to('http://www.foo.com/bar/1') ->asJson() ->delete();
发送HEAD请求
HEAD请求与 GET 请求类似,但使用 head() 方法代替
use LaravelMasterPackage\Curl\Facades\Curl; // Send a HEAD request to: http://www.foo.com/bar/1 $response = Curl::to('http://www.foo.com/bar/1') ->head(); // Send a HEAD request to: http://www.foo.com/bar/1?foz=baz $response = Curl::to('http://www.foo.com/bar/1') ->withData( array( 'foz' => 'baz' ) ) ->head();
发送自定义头部
使用 withHeader() 方法发送自定义头部很容易。可以通过链式调用多次调用以向请求添加多个头部
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar with 2 custom headers $response = Curl::to('http://foo.com/bar') ->withHeader('MyFirstHeader: 123') ->withHeader('MySecondHeader: 456') ->get();
或者,您可以使用 withHeaders() 将多个头部合并到单个方法调用中
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar with 2 custom headers $response = Curl::to('http://foo.com/bar') ->withHeaders( array( 'MyFirstHeader: 123', 'MySecondHeader: 456' ) ) ->get();
您还可以在调用withHeaders()方法时使用键值对。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar with 2 custom headers $response = Curl::to('http://foo.com/bar') ->withHeaders( array( 'MyFirstHeader' => '123', 'MySecondHeader' => '456' ) ) ->get();
对于(携带者)授权头,您也可以使用特定的实用方法。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar with "Authorization: 123" header $response = Curl::to('http://foo.com/bar') ->withAuthorization('123') ->get(); // Send a GET request to: http://www.foo.com/bar with "Authorization: Bearer 123" header $response = Curl::to('http://foo.com/bar') ->withBearer('123') ->get();
请注意,如果添加了相同的头多次,它们会相互覆盖。
指定内容类型
使用withContentType()方法发送自定义头非常简单。可以通过链式调用多次调用以向请求添加多个头。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar with a json content type $response = Curl::to('http://foo.com/bar') ->withContentType('application/json') ->get();
使用代理
如果您需要通过代理发送请求,可以使用'withProxy()'方法。此方法需要五个参数
- 代理URL(必需)
- 端口(可选)
- 代理方案类型(可选,例如
http://、https://等) - 用户名(可选)
- 密码(可选)
如果未填写,则忽略可选参数。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to: http://www.foo.com/bar with a json content type $response = Curl::to('http://foo.com/bar') ->withProxy('192.168.1.1', 80, 'http://', 'Foo', 'Bar') ->get();
通过Curl发送文件
对于通过POST请求发送文件,您可以使用withFile方法在发送之前正确格式化请求。
use LaravelMasterPackage\Curl\Facades\Curl; $response = Curl::to('http://foo.com/bar') ->withData( array( 'Foo' => 'Bar' ) ) ->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' ) ->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' ) ->post();
您可以根据需要向请求添加任意数量的文件。以下几点需要注意
- 提交文件时,不能使用
asJson()方法和asJsonRequest()方法。如果您这样做,文件将无法正确传输。 - 文件通过
withFile()方法的第一参数添加到withData()方法提供的数据中。如果此键已存在,则会被覆盖。
下载文件
对于下载文件,您可以使用download()方法。
use LaravelMasterPackage\Curl\Facades\Curl; // Download an image from: file http://www.foo.com/bar.png $response = Curl::to('http://foo.com/bar.png') ->withContentType('image/png') ->download('/path/to/dir/image.png');
使用响应对象
默认情况下,包只会返回请求的内容。在某些情况下,了解额外的请求信息可能也很有用,例如HTTP状态码和可能发生的错误消息。在这种情况下,您可以使用returnResponseObject()方法,它将返回一个包含额外信息以及响应内容的stdClass。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to http://www.foo.com/bar and return a response object with additional information $response = Curl::to('http://www.foo.com/bar') ->returnResponseObject() ->get(); $content = $response->content;
响应对象将如下所示
{
"content": "Message content here",
"status": 200,
"contentType": "content-type response header (ex: application/json)",
"error": "Error message goes here (Only added if an error occurs)"
}
响应头
在某些情况下,可能需要将响应头返回给用户。这可以通过使用withResponseHeaders()方法轻松完成。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to http://www.foo.com/bar and return a response object with additional information including response headers $response = Curl::to('http://www.foo.com/bar') ->withResponseHeaders() ->returnResponseObject() ->get(); $content = $response->content; $headers = $response->headers;
响应对象将如下所示
{
"content": "Message content here",
"status": 200,
"contentType": "content-type response header (ex: application/json)",
"error": "Error message goes here (Only added if an error occurs)",
"headers": {
"header-type-1": "header-content-1",
"header-type-2": "header-content-2"
}
}
请注意,必须与returnResponseObject()方法一起使用withResponseHeaders()方法才能看到返回的头。
调试请求
如果请求失败,可能需要调试请求。在这种情况下,您可以使用enableDebug()方法。此方法使用一个参数,即要存储调试信息的文件名。
use LaravelMasterPackage\Curl\Facades\Curl; // Send a GET request to http://www.foo.com/bar and log debug information in /path/to/dir/logFile.txt $response = Curl::to('http://www.foo.com/bar') ->enableDebug('/path/to/dir/logFile.txt') ->get();
使用cURL选项
您可以使用多个实用方法(如为添加头使用withHeader())将各种cURL选项添加到请求中,或者如果没有适用的实用方法,可以使用通用的withOption()方法。包将自动将选项以CURLOPT_前缀预置。值得注意的是,包不对cURL选项进行任何验证。有关可用cURL选项的更多信息,请参阅此处。
有关参数和返回类型的特定信息,我鼓励您查看laravelmasterpackage\curl\src\LaravelMasterPackage\Curl\Builder.php。此类具有广泛的doc blocks,其中包含每个特定方法的必要信息。
不使用Laravel的使用方法
不使用Laravel的使用方法与之前描述的方法相同。唯一的区别是,您将无法使用外观来访问CurlService。
$curlService = new \LaravelMasterPackage\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(); // Send a PUT request to: http://www.foo.com/bar $response = $curlService->to('http://www.foo.com/bar') ->put(); // Send a DELETE request to: http://www.foo.com/bar $response = $curlService->to('http://www.foo.com/bar') ->delete(); // Send a HEAD request to: http://www.foo.com/bar $response = $curlService->to('http://www.foo.com/bar') ->head();
计划
- 为其他cURL选项添加额外的实用方法。
- 添加合约,允许使用不同的HTTP提供程序,如Guzzle。
支持
通过Patreon支持我,帮助我进一步开发和维护此包!
许可证
本软件包是开源软件,许可协议为MIT许可协议
联系方式
有关软件包的问题、错误、建议和/或功能请求,请使用GitHub问题系统并/或提交拉取请求。提交问题时,请始终提供您问题的详细说明、任何收到的响应或反馈、可能相关的日志消息以及演示问题的源代码示例。如果不提供这些信息,我可能无法帮助您解决问题。请在提交问题或拉取请求之前查看贡献指南。
对于任何其他问题,请随时使用以下凭据
Jan Oris(开发者)
- 邮箱:jan.oris@laravelmasterpackage.be
- 电话:+32 496 94 20 57