ixudra / curl
为Laravel框架定制的PHP cURL库 - 由Ixudra开发
Requires
- php: >=5.4.0
- ext-curl: *
- illuminate/support: >=4.0
This package is auto-updated.
Last update: 2024-09-22 19:28:24 UTC
README
为Laravel框架定制的PHP cURL库 - 由Ixudra开发。
该包提供了一个易于使用的接口,用于从您的PHP Web应用程序发送cURL请求。该包提供了一个直观、流畅的接口,类似于Laravel查询构造器,可以轻松配置请求。此外,还有一些实用方法,允许您轻松地将某些选项添加到请求中。这使得创建和使用cURL请求更加容易,也使您的代码更具可读性。
提供的功能完全独立于框架,但同时也包含一个Laravel服务提供器,以便轻松集成到您的Laravel项目中。
发布问题前注意:在发布关于该包的问题时,请务必提供尽可能多的关于请求的信息。这包括您尝试将示例cURL请求转换为包语法的示例,您的实际包语法(完整的请求),以及(如果可能)一个我可以用来测试请求的示例URL。
安装
通过Composer引入此包。
{ "require": { "ixudra/curl": "6.*" } }
或者在终端中运行:composer require ixudra/curl
Laravel 5.5+集成
Laravel的包发现将为您处理集成。
Laravel 5.*集成
将服务提供器添加到您的config/app.php
文件中
'providers' => array( //... Ixudra\Curl\CurlServiceProvider::class, ),
将外观添加到您的config/app.php
文件中
'aliases' => array( //... 'Curl' => Ixudra\Curl\Facades\Curl::class, ),
Laravel 4.*集成
将服务提供器添加到您的app/config/app.php
文件中
'providers' => array( //... 'Ixudra\Curl\CurlServiceProvider', ),
将外观添加到您的app/config/app.php
文件中
'facades' => array( //... 'Curl' => 'Ixudra\Curl\Facades\Curl', ),
Lumen 5.*集成
在您的bootstrap/app.php
中,确保已取消注释以下行(大约在第26行)
$app->withFacades();
然后,注册您的类别名
class_alias('Ixudra\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(Ixudra\Curl\CurlServiceProvider::class);
不使用Laravel的集成
在您想使用此包的地方创建一个CurlService
的新实例
$curlService = new \Ixudra\Curl\CurlService();
用法
Laravel用法
该包提供了一个易于使用的接口,用于从您的应用程序发送cURL请求。该包提供了一个流畅的接口,类似于Laravel查询构造器,可以轻松配置请求。有几个实用方法允许您轻松地将某些选项添加到请求中。如果没有适用的实用方法,您也可以使用通用的withOption
方法。
发送GET请求
为了发送GET请求,您需要使用包提供的get()
方法
use Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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()
方法。如果使用,文件将无法正确传输 - 文件被添加到使用
withData()
方法提供的withFile()
方法的第一个参数中的数据。如果此键已存在,则会被覆盖。
下载文件
对于下载文件,您可以使用 download()
方法
use Ixudra\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 Ixudra\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 Ixudra\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 Ixudra\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()
向请求添加一个头部)或使用通用的 withOption()
方法(如果没有适用的实用方法)向请求添加各种 cURL 选项。包将自动在选项前面添加 CURLOPT_
前缀。请注意,包不会对 cURL 选项进行任何验证。有关可用的 cURL 选项的更多信息,请参阅 此处。
有关参数和返回类型的特定信息,我鼓励您查看 ixudra\curl\src\Ixudra\Curl\Builder.php
。此类包含详细的注释,包含每个特定方法的必要信息。
无 Laravel 使用
无 Laravel 的使用与之前描述的使用相同。唯一的区别是您将无法使用外观来访问 CurlService
。
$curlService = new \Ixudra\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问题系统并/或提交一个pull请求。提交问题时,请始终提供详细的问题说明,任何获得的回应或反馈,可能相关的日志消息以及展示问题的源代码示例。如果不提供这些信息,我可能无法帮助您解决问题。在提交问题或pull请求之前,请先查看贡献指南。
对于任何其他问题,请自由使用以下凭证
Jan Oris(开发者)
- 电子邮件:jan.oris@ixudra.be
- 电话:+32 496 94 20 57