neoan3-apps / curl
neoan3 curl class
Requires
- ext-curl: *
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-11-12 08:19:43 UTC
README
Simple PHP curl wrapper
This lightweight curl wrapper facilitates most common needs and is designed for server-2-server communication for Oauth flows, APIs using JSON responses and alike.
Installation
composer require neoan3-apps/curl
Quick start
Json responses are automatically decoded to associative arrays.
try{ $comments = \Neoan3\Apps\Curl::get('https://jsonplaceholder.typicode.com/posts/1/comments'); } catch (CurlException $e){ echo $e->getMessage(); } /* output * [ * ['postId' => 1, 'name' => 'labore ...'] * [...] * ] * */
Simplified calls
get($url, $array = [], $auth = false, $authType = 'Bearer')
NOTE: $array is converted to GET parameters
post($url, $array = [], $auth = false, $authType = 'Bearer')
put($url, $array = [], $auth = false, $authType = 'Bearer')
These calls are most common and can either be used with or without authorization. If $auth is set, the methods assume a Baerer token.
curling($url, $arrayOrBody, $header, $type = 'POST')
Custom call where the header is set manually as an array and the method defaulting to POST
setResponseFormatVerbose()
The class defaults to "plain" output only containing the payload of the response. this method changes the behavior to return responses in the following format:
[ 'headers' => $headers, // array 'body' => $responseBody, // array 'status' => $status // int (e.g.200) ];
When switching between output formats, use setResponseFormatPlain() to reset behavior.
Exceptions
CurlException is only thrown if responses are 500 & above. This means that a 404 is a valid call and evaluation of usefulness should be done elsewhere.
SECURITY: While I am sure your vendor-folder is probably already protected, neoan3-curl protects the _log folder with an .htaccess file. Should you not use Apache and your vendor folder is visible, please take steps to secure the folder vendor/neoan3-apps/curl/_log