Eksempler
Det er ret enkelt at kode op mod API’et. Herunder kan du se eksempler på at hente, oprette, opdatere og slette data fra henholdsvis C# og PHP. Eksemplerne er generelle – for at tilpasse dem til de enkelte funktionskald der er beskrevet på de andre sider, skal du bare udskifte URLen, verben og selvfølgelig de data der sendes.
C# kodeeksempler
Hent kunde
WebRequest request = WebRequest.Create(“https://app.tasks.dk/api/client/x”);
request.Headers.Add(“Authorization”, “din-api-key”);
request.Method = “GET”;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
// hent svar-xml, fx via reader.ReadToEnd()
reader.Close();
response.Close();
Opret kunde
string data = “<data><name>Verdensfirmaet A/S</name></data>”;
WebRequest request = WebRequest.Create(“https://app.tasks.dk/api/clients”);
request.Headers.Add(“Authorization”, “din-api-key”);
request.Method = “POST”;
request.ContentLength = data.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
// hent svar-xml, fx via reader.ReadToEnd()
reader.Close();
response.Close();
Opdater kunde
string data = “<data><name>Verdensfirmaet A/S</name><email>info@verdensfirmaet.dk</email></data>”;
WebRequest request = WebRequest.Create(“https://app.tasks.dk/api/client/x”);
request.Headers.Add(“Authorization”, “din-api-key”);
request.Method = “PUT”;
request.ContentLength = data.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
// hent svar-xml, fx via reader.ReadToEnd()
reader.Close();
response.Close();
Slet kunde
WebRequest request = WebRequest.Create(“https://app.tasks.dk/api/client/x”);
request.Headers.Add(“Authorization”, “din-api-key”);
request.Method = “DELETE”;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
// hent svar-xml, fx via reader.ReadToEnd()
reader.Close();
response.Close();
PHP kodeeksempler
Hent kunde
$url = ‘https://app.tasks.dk/api/client/x’;
$session = curl_init();
curl_setopt($session, CURLOPT_HTTPHEADER, array(‘Authorization: din-api-key’));
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, ‘GET’);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
// $response indeholder nu svar-xml
Opret kunde
$url = ‘https://app.tasks.dk/api/clients’;
$xml = ‘<data><name>Verdensfirmaet A/S</name></data>’;
$session = curl_init();
curl_setopt($session, CURLOPT_HTTPHEADER, array(‘Authorization: din-api-key’));
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_POSTFIELDS, $xml);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
// $response indeholder nu svar-xml
Opdater kunde
$url = ‘https://app.tasks.dk/api/client/x’;
$xml = ‘<data><name>Verdensfirmaet A/S</name><email>info@verdensfirmaet.dk</email></data>’;
$session = curl_init();
curl_setopt($session, CURLOPT_HTTPHEADER, array(‘Authorization: din-api-key’));
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_POSTFIELDS, $xml);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, ‘PUT’);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
// $response indeholder nu svar-xml
Slet kunde
$url = ‘https://app.tasks.dk/api/client/x’;
$session = curl_init();
curl_setopt($session, CURLOPT_HTTPHEADER, array(‘Authorization: din-api-key’));
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, ‘DELETE’);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
// $response indeholder nu svar-xml