67 lines
2.3 KiB
PHP
67 lines
2.3 KiB
PHP
<?php
|
|
|
|
class WeFactAPI
|
|
{
|
|
private $url;
|
|
private $apiKey;
|
|
|
|
/**
|
|
* Constructor die de API sleutel en URL ontvangt.
|
|
* @param string $apiKey Jouw WeFact API sleutel.
|
|
* @param string $apiUrl De basis-URL voor de WeFact API.
|
|
*/
|
|
public function __construct(string $apiKey, string $apiUrl = 'https://api.mijnwefact.nl/v2/')
|
|
{
|
|
$this->url = $apiUrl;
|
|
$this->apiKey = $apiKey;
|
|
}
|
|
|
|
/**
|
|
* Verstuurt een verzoek naar de WeFact API.
|
|
* @param string $controller De naam van de controller (bv. 'debtor').
|
|
* @param string $action De actie die uitgevoerd moet worden (bv. 'add').
|
|
* @param array $params Een array met parameters voor de call.
|
|
* @return array Het antwoord van de API.
|
|
*/
|
|
public function sendRequest(string $controller, string $action, array $params): array
|
|
{
|
|
// Voeg de verplichte parameters toe
|
|
$params['api_key'] = $this->apiKey;
|
|
$params['controller'] = $controller;
|
|
$params['action'] = $action;
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, '10');
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
|
|
|
|
// Let op: SSL-verificatie nooit uitschakelen in een productieomgeving.
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
|
|
$curlResp = curl_exec($ch);
|
|
$curlError = curl_error($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($curlError != '') {
|
|
$result = [
|
|
'controller' => 'invalid', 'action' => 'invalid',
|
|
'status' => 'error', 'date' => date('c'),
|
|
'errors' => [$curlError]
|
|
];
|
|
} elseif ($httpCode >= 400) { // Vang alle client/server fouten af
|
|
$result = [
|
|
'controller' => 'invalid', 'action' => 'invalid',
|
|
'status' => 'error', 'date' => date('c'),
|
|
'errors' => ["HTTP status code: " . $httpCode, $curlResp]
|
|
];
|
|
} else {
|
|
$result = json_decode($curlResp, true);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
} |