chillerlan / php-curl
A tiny cURL wrapper. PHP 7+
Requires
- php: >=7.0.7
- chillerlan/php-traits: ^1.1
Requires (Dev)
- phpunit/phpunit: ^6.5
README
A simple cURL wrapper, mostly for API scraping purposes.
Features:
- No PSR-7!
- No 87 extra layers of abstraction!
- No fancy!
In case you're looking for that: go along, use Guzzle instead.
Requirements
- PHP 7+
- the cURL extension
- probably a CA certificate bundle
Documentation
Installation
requires composer
composer.json
You can simply clone the repo and run composer install
in the root directory.
In case you want to include it elsewhere, just add the following to your composer.json
(note: replace dev-master
with a version boundary):
{ "require": { "php": ">=7.0.7", "chillerlan/php-curl": "dev-master" } }
Manual installation
Download the desired version of the package from master or release and extract the contents to your project folder. After that:
- run
composer install
to install the required dependencies and generate/vendor/autoload.php
. - if you use a custom autoloader, point the namespace
chillerlan\TinyCurl
to the foldersrc
of the package
Profit!
Usage
Request
The most simple way:
use chillerlan\TinyCurl\{Request, URL}; $response = (new Request)->fetch(new URL('http://example.url/path')); //do stuff $json = $response->json;
Ways to set options:
use chillerlan\TinyCurl\{Request, RequestOptions, URL}; $options = new RequestOptions; $options->ca_info = '/path/to/cacert.pem'; // while creating the Request instance $request = new Request($options); // on an existing Request instance: $request->setOptions($options); // ...
The Request
class also features a method to easily extract short URLs:
use chillerlan\TinyCurl\Request; (new Request)->extractShortUrl('https://t.co/ZSS6nVOcVp');
RequestTrait
The RequestTrait
is a somewhat stripped down variant to quick and easy implement. Using the trait in your own classes:
class MyClass{ use RequestTrait; public function __construct(){ $this->setRequestCA('/path/to/cacert.pem'); } public function doStuff(){ $response = $this->fetch(new URL('https://example.url/path')); } }
MultiRequest
The MultiRequest
is an implementation of curl_multi
a.k.a. "rolling cURL" to process multiple cURL requests
in parallel, non-blocking. Please refer to the tests & examples and my GW2 database for further details on the implementation.
Properties of RequestOptions
property | type | default | allowed | description |
---|---|---|---|---|
$user_agent |
string | 'chillerLAN-php-curl' | * | the user agent used for each request |
$timeout |
int | 10 | * | request timeout |
$curl_options |
array | [] |
[*] |
cURL options for each instance |
$ca_info |
string | null | * | path to a cacert |
$max_redirects |
int | 0 | * | maximum redirects |
Properties of MultiRequestOptions
The MultiRequestOptions
object extends RequestOptions
with the following properties:
property | type | default | allowed | description |
---|---|---|---|---|
$handler |
string | null | * | an optional handler FQCN (implements MultiResponseHandlerInterface ) |
$window_size |
int | 5 | * | maximum of concurrent requests |
$sleep |
int | 100 | * | sleep timer (milliseconds) between each fired request on startup |
Disclaimer!
I don't take responsibility for molten phone lines, bloated hard disks, self-induced DDoS, broken screens etc. Use at your own risk! ;)