phpgt / curlinterface
cURL object wrapper.
Fund package maintenance!
PhpGt
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- phpgt/json: ^1.2
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
- dev-master
- v3.1.1
- v3.1.0
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.x-dev
- v2.1.0
- v2.0.0
- v1.1.0
- v1.0.0
- dev-dependabot/composer/phpunit/phpunit-10.1.3
- dev-dependabot/composer/phpunit/phpunit-10.1.2
- dev-test-github
- dev-codesniffer
- dev-54-httplug
- dev-circleci
This package is auto-updated.
Last update: 2023-05-12 03:58:25 UTC
README
This library wraps PHP's native cURL extension functions with objects, for better code readability and testability.
Why? We wanted to lay an object oriented foundation for PHP.Gt/Fetch, our PHP implementation of the web's fetch API that uses cURL to create asynchronous HTTP calls with promises.
Example usage: Get a JSON object from a remote source
When working with HTTP calls, it is extremely common to work with JSON. This library removes the need of a lot of boilerplate code by buffering the output of exec()
calls for easy retrieval later with output()
or outputJson()
.
Example using PHP.Gt/Curl:
$curl = new Curl("https://catfact.ninja/fact"); $curl->exec(); $json = $curl->outputJson(); echo "Here's a cat fact: {$json->getString("fact")}"; echo PHP_EOL; echo "The fact's length is {$json->getInt("length")} characters."; echo PHP_EOL;
Same example using PHP's native curl_*
functions:
// Using native functionality to achieve the same: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://catfact.ninja/fact"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); if(false === $result) { die("CURL error: " . curl_error($ch)); } $json = json_decode($result); if(is_null($json)) { die("JSON decoding error: " . json_last_error_msg()); } // Note: No type checks are made on the `fact` and `length` properties here. echo "Here's a cat fact: {$json->fact}"; echo PHP_EOL; echo "The fact's length is {$json->length} characters."; echo PHP_EOL;