Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpgt/curl
Object oriented cURL wrapper.
https://github.com/phpgt/curl
curl curl-multi oop-library php-7 php-curl php-curl-async php-interface phpgt wrapper
Last synced: 3 months ago
JSON representation
Object oriented cURL wrapper.
- Host: GitHub
- URL: https://github.com/phpgt/curl
- Owner: PhpGt
- License: mit
- Created: 2016-10-31T19:05:00.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-05-12T03:58:21.000Z (over 1 year ago)
- Last Synced: 2024-10-12T12:21:54.859Z (3 months ago)
- Topics: curl, curl-multi, oop-library, php-7, php-curl, php-curl-async, php-interface, phpgt, wrapper
- Language: PHP
- Homepage: https://php.gt/curl
- Size: 229 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
cURL object wrapper.
====================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](https://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:
--------------------------```php
$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:
---------------------------------------------------```php
// 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;
```