anlutro / curl
简单的 OOP cURL 封装。
1.5.2
2023-06-27 07:39 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
- dev-master
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.1
- 0.1.0
- dev-impl-php8
- dev-fix-http2-test
- dev-fix-curlopt-file-again
- dev-fix-curlopt-file
- dev-impl-github-actions
- dev-impl-http-auth
- dev-fix-encoded-data
- dev-develop
This package is auto-updated.
Last update: 2024-08-27 10:05:19 UTC
README
PHP cURL 功能的最小 OOP 封装。
请注意,这不是作为一个高级抽象。您仍然需要了解“纯 PHP”cURL 的工作原理,您需要了解要设置的 cURL 选项,以及您需要了解一些 HTTP 基础知识。
如果您在寻找一个更友好的抽象,请查看 Guzzle。
安装
$ composer require anlutro/curl
用法
$curl = new anlutro\cURL\cURL; $response = $curl->get('http://www.google.com'); // easily build an url with a query string $url = $curl->buildUrl('http://www.google.com', ['s' => 'curl']); $response = $curl->get($url); // the post, put and patch methods takes an array of POST data $response = $curl->post($url, ['api_key' => 'my_key', 'post' => 'data']); // add "json" to the start of the method to convert the data to a JSON string // and send the header "Content-Type: application/json" $response = $curl->jsonPost($url, ['post' => 'data']); // if you don't want any conversion to be done to the provided data, for example // if you want to post an XML string, add "raw" to the start of the method $response = $curl->rawPost($url, '<?xml version...'); // raw request are also the easiest way to upload files $file = curl_file_create('/path/to/file'); $response = $curl->rawPost($url, ['file' => $file]); // a response object is returned var_dump($response->statusCode); // response status code integer (for example, 200) var_dump($response->statusText); // full response status (for example, '200 OK') echo $response->body; var_dump($response->headers); // array of headers var_dump($response->info); // array of curl info
如果您需要发送头信息或设置 cURL 选项,您可以直接操作请求对象。send()
完成请求并返回结果。
// newRequest, newJsonRequest and newRawRequest returns a Request object $request = $curl->newRequest('post', $url, ['foo' => 'bar']) ->setHeader('Accept-Charset', 'utf-8') ->setHeader('Accept-Language', 'en-US') ->setOption(CURLOPT_CAINFO, '/path/to/cert') ->setOption(CURLOPT_FOLLOWLOCATION, true); $response = $request->send();
您还可以使用 setHeaders(array $headers)
和 setOptions(array $options)
来替换所有现有选项。
请注意,在调用 send()
时,某些 cURL 选项会被重置。查看 cURL::prepareMethod
方法的源代码以了解哪些选项被覆盖的完整概述。
HTTP 基本认证
$request = $curl->newRequest('post', $url, ['foo' => 'bar']) ->setUser($username)->setPass($password);
Laravel
如果更喜欢静态方法调用而不是依赖注入,则该软件包附带了一个您可以使用的外观。
您不需要添加服务提供者。
可选地,将 'cURL' => 'anlutro\cURL\Laravel\cURL'
添加到 config/app.php
中的别名数组中。
在上面的示例中将 $curl->
替换为 cURL::
。
联系方式
如果您有任何问题或建议,请打开 GitHub 上的问题。
许可
本存储库的内容在 MIT 许可 下发布。