Skip to content

Instantly share code, notes, and snippets.

@alexjeen
Created December 30, 2016 21:21
Show Gist options
  • Save alexjeen/6211c363c4efd4c3034cb3f81f7520bf to your computer and use it in GitHub Desktop.
Save alexjeen/6211c363c4efd4c3034cb3f81f7520bf to your computer and use it in GitHub Desktop.
Exact Online API XML read + write
<?php
namespace app\exact;
use GuzzleHttp\Client;
class RawConnection extends \Picqer\Financials\Exact\Connection
{
/**
* Gets XML for an division.
*
* @param string $division division code
* @param string $topic the topic
*
* @return false on failure or SimpleXMLElement
*/
public function getXml($division, $topic)
{
$headers = ['Authorization' => 'Bearer '.$this->getAccessToken()];
$client = new Client([
'base_uri' => 'https://start.exactonline.nl/docs/',
]);
try {
$response = $client->request('GET', 'XMLDownload.aspx', [
'query' => ['Topic' => $topic, '_Division_' => $division],
'headers' => $headers,
]);
return new \SimpleXMLElement((string) $response->getBody());
} catch (\Exception $e) {
}
return false;
}
/**
* Gets XML for an division.
*
* @param string $division division code
* @param string $topic the topic
*
* @return false on failure or SimpleXMLElement
*/
public function postXml($division, $topic, $body)
{
$headers = ['Authorization' => 'Bearer '.$this->getAccessToken()];
$client = new Client([
'base_uri' => 'https://start.exactonline.nl/docs/',
]);
try {
$response = $client->request('POST', 'XMLUpload.aspx', [
'query' => ['Topic' => $topic, '_Division_' => $division],
'headers' => $headers,
'body' => $body,
]);
return new \SimpleXMLElement((string) $response->getBody());
} catch (\Exception $e) {
var_dump($e->getMessage());
}
return false;
}
}
@LexKoomen
Copy link

LexKoomen commented Feb 10, 2017

I updated your getXml function because you would only get the first 100 records.

public function getXml($division, $topic, $tsp) {   
    $array = array();
    $i = 0;
    $x = true;
    while ($x) {
        $headers = ['Authorization' => 'Bearer '.$this->getAccessToken()];
        $client = new Client([
            'base_uri' => 'https://start.exactonline.nl/docs/',
        ]);
        if (isset($tspaging)) {
            $tsp = $tspaging;
            unset($tspaging);
        }
        try {
            $response = $client->request('GET', 'XMLDownload.aspx', [
                'query' => ['Topic' => $topic, '_Division_' => $division, 'TSPaging' => $tsp],
                'headers' => $headers,
            ]);
            $string = new \SimpleXMLElement((string) $response->getBody());
            $tspaging = strval($string->Topics->Topic->attributes()->{'ts_d'});
            $array[$i]=$string;
        } 
        catch (\Exception $e) {}
        if (!isset($tspaging)) {
            $x = false;
        }
        $i++;
        unset($string);
        unset($response);
        unset($client);
    }
    return $array;
}

This will get the paging value out of the response and loop it into an array so you would loop through all items in your Exact Online database.

@justijndepover
Copy link

justijndepover commented Dec 7, 2017

@LexKoomen There's a mistake in your code:

$tspaging = strval($string->Topics->Topic->attributes()->{'ts_d'});
$array[$i]=$string;

You should check whether or not the Topic value exists before calling the attributes function.

if(isset($string->Topics->Topic)){
    $tspaging = strval($string->Topics->Topic->attributes()->{'ts_d'});
    $array[$i]=$string;
}

@MranalineeStudioHenk
Copy link

MranalineeStudioHenk commented Jan 3, 2022

@justijndepover @LexKoomen How can we add filter and change limit from 100 to 1000 ????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment