Example code
Example code
The code below is an example that retrieves the transactions for the current month of the publisher account.
Please also check out these code samples on Github:
- Github Daisycon oAuthExamples
- Github SamanthaAdrichem/DaisyconApi
- Github: edwinheij/daisycon (currently deprecated)
Example PHP code – Getting your transactions
Please note, you will need to setup your own oAuth handshake. This example assumes you have a function that provides the tokens and refreshes them when expired.
For examples of token refresh and expiry checkout our examples above
- $token = getOAuthToken(); // Returns a JWT token that’s not expired
- $publishers = performCall($token, ‘/publishers’);
- foreach ($publishers as $publisher)
- {
- $start = date(‘Y-m-01’);
- $end = date(‘Y-m-d’);
- echo ‘Fetching transactions for: ‘ . $publisher->name . ‘ (‘ . $publisher->id . ‘) – ‘ . $start . ‘ – ‘ . $end, PHP_EOL;
- $collection = [];
- $page = 1;
- $perPage = 1000;
- $pages = 1;
- while ($page <= $pages)
- {
- echo ‘fetching page: ‘ . $page . ‘ out of ‘ . ($page === 1 ? ‘?’ : $pages), PHP_EOL;
- $responseHeaders = [];
- $transactions = performCall(
- $token,
- ‘/publisher/’ . $publisher->id . ‘/transactions’,
- [
- ‘start’ => $start,
- ‘end’ => $end,
- ‘page’ => $page,
- ‘per_page’ => $perPage,
- ],
- $responseHeaders
- );
- if ($page === 1)
- {
- $pages = ceil($responseHeaders[‘X-Total-Count’] / $perPage);
- }
- $collection = array_merge($collection, $transactions);
- }
- var_dump($collection);
- exit;
- }
- function performCall($token, $relativeUrl, $data = [], &$responseHeaders = [])
- {
- $requestUrl = ‘https://services.daisycon.com’ . $relativeUrl;
- $curlHandler = curl_init();
- $requestHeaders = [
- “Authorization: Bearer {$token}“,
- ‘Content-Type: application/json’,
- ];
- $paramSeparator = false === stripos($requestUrl, ‘?’) ? ‘?’ : ‘&’;
- $requestUrl .= $paramSeparator . http_build_query($data);
- curl_setopt($curlHandler, CURLOPT_URL, $requestUrl);
- curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $requestHeaders);
- curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
- curl_setopt(
- $curlHandler,
- CURLOPT_HEADERFUNCTION,
- function ($curlHandler, $responseHeader) use (&$responseHeaders) {
- @list($headerName, $headerValue) = explode(‘: ‘, $responseHeader);
- if (true === str_starts_with($headerName, ‘X-‘))
- {
- $responseHeaders[$headerName] = trim($headerValue);
- }
- return strlen($responseHeader);
- }
- );
- $response = curl_exec($curlHandler);
- $returnResponse = @json_decode($response);
- curl_close($curlHandler);
- return $returnResponse;
- }