palmtree / curl
Curl 组件用于 Palmtree PHP
v2.0.0
2020-02-02 15:47 UTC
Requires
- php: >=7.1
- ext-curl: *
Requires (Dev)
- ext-sockets: *
- palmtree/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^8.1|^7.5
Suggests
- ext-json: For POSTing JSON data
README
这是一个 PHP cURL 封装器,可以简化 HTTP 请求。
要求
- PHP >= 7.1
安装
使用 composer 将此包添加到您的依赖项中
composer require palmtree/curl
使用方法
基本使用
如果您只想从 URL 获取响应体,可以使用静态的 getContents
方法
<?php use Palmtree\Curl\Curl; $contents = Curl::getContents('https://example.org');
如果您想访问响应头和体,请创建一个新的实例
<?php use Palmtree\Curl\Curl; $curl = new Curl('https://example.org'); // Returns the response body when used as a string echo $curl; $response = $curl->getResponse(); $headers = $response->getHeaders(); $contentType = $response->getHeader('Content-Type'); $body = $response->getBody();
高级使用
<?php use Palmtree\Curl\Curl; $curl = new Curl('https://example.org', [ CURLOPT_FOLLOWLOCATION => true, ]); $curl->getRequest()->addHeader('Host', 'example.org'); try { $response = $curl->execute(); } catch(CurlErrorException $e) { } $headers = $response->getHeaders(); $body = $response->getBody(); if($response->is404()) { // handle 404 error } if($response->isOk()) { // response status code is in the 2xx range }
许可证
在 MIT 许可证 下发布