Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/electrikmilk/php-api-class
My own API class, some parts borrowed from php-curl-class.
https://github.com/electrikmilk/php-api-class
api curl php
Last synced: about 6 hours ago
JSON representation
My own API class, some parts borrowed from php-curl-class.
- Host: GitHub
- URL: https://github.com/electrikmilk/php-api-class
- Owner: electrikmilk
- License: unlicense
- Created: 2021-10-28T15:43:52.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-13T00:16:38.000Z (almost 3 years ago)
- Last Synced: 2023-03-08T19:11:06.130Z (almost 2 years ago)
- Topics: api, curl, php
- Language: PHP
- Homepage:
- Size: 58.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP API Class
My custom API class, some parts borrowed from [php-curl-class](https://github.com/php-curl-class/php-curl-class).## Basic Usage
```php
$service = new API($api_key = 'Basic {YOUR_API_KEY}',$base_url = 'https://api.example.com/v1/', $send_json = true | false);
$service->get('endpoint');
```## Options
```php
$service->header('key','value'); // set header
$service->opt(CURLOPT_SOMETHING,'value'); // set option
$service->json = true || false; // send data with json_encode() or http_build_query()
```## Requests
Use `get()`, `post()`, `patch()`, `put()`, or `delete()` method. All of them can be given a fields array. Each of them starts a new request, meaning the previous one is discarded.
```php
$data = $service->post('endpoint',array(
"key"=>"value"
));
// if response and http success code (200), returns response.
// if response and http error code, returns null, response is accessed via the error() method.
// if no response, returns http code.// starts a new request, any methods or variables after this will be based on this request
$data = $service->delete("endpoint/{$id}");if($service->http_code === 200) {
echo "Response: ({$service->http_code}): {$service->response()}";
} else {
echo $service->error();
// return curl_error() if there is one.
// returns response, if http error code.
// otherwise, returns null.
}
```