palmtree / curl
Curl component for Palmtree PHP
Installs: 17
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/palmtree/curl
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
A PHP cURL wrapper to make HTTP requests easier.
Requirements
- PHP >= 7.1
Installation
Use composer to add the package to your dependencies:
composer require palmtree/curl
Usage
Basic Usage
You can use the static getContents
method if you just want to retrieve a response body from a URL:
<?php use Palmtree\Curl\Curl; $contents = Curl::getContents('https://example.org');
If you want access to the response headers and body, create a new instance instead:
<?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();
Advanced Usage
<?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 }
License
Released under the MIT license