Examples of how to fetch a guideline with our API in various languages

Below you get two examples of how to fetch a guideline using our API

We use the example username: apitest@magicapp.org , and password: apitest

 PHP

<?php

/*
 * php.ini needs -
 *
 * curl.cainfo to be point to a valid pem file
 *
 */

$username = "apitest@magicapp.org";
$password = "apitest";
$base_url = "https://www.magicapp.org/";
$base_api_url = $base_url."api/v1/";
$auth_url = $base_url."/authenticate";
$xsrf_token_name = "XSRF-TOKEN";


// get xsrf token

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
 
$response = curl_exec($ch);
if (curl_errno($ch)) die(curl_error($ch));

preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$cookies = array();
foreach($matches[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}
$xsrf_token = $cookies[$xsrf_token_name];

print("xsrf token: ". $xsrf_token ."\n");


// get auth token

$auth_post = "username=".urlencode($username)."&password=".urlencode($password);
$auth_header = array("X-XSRF-TOKEN: ".$xsrf_token , "Cookie: XSRF-TOKEN" => $xsrf_token );
$auth_cookie = "XSRF-TOKEN=".$xsrf_token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $auth_post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth_header);
curl_setopt($ch, CURLOPT_COOKIE, $auth_cookie);

$result = curl_exec($ch);
if (curl_errno($ch)) print curl_error($ch);

$json = json_decode($result, true);
$auth_token = $json['token'];

print("auth token: ". $auth_token ."\n");


// get data !

$auth_header = array_merge($auth_header, array("X-Auth-Token: ". $auth_token));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_api_url."/guidelines/2");
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth_header);
curl_setopt($ch, CURLOPT_REFERER, $auth_url);
   
$result = curl_exec($ch);
var_dump(json_decode($result, true));
   
curl_close($ch);
   
?>

 

Python


import requests
import json
auth = {'username': 'apitest@magicapp.org', 'password': 'apitest'}
url = 'https://www.magicapp.org/authenticate'

r = requests.options(url)

token = r.cookies['XSRF-TOKEN']
cookies = {'XSRF-TOKEN': token}
headers = {'X-XSRF-TOKEN': token, 'Referer': url}

r = requests.post(url, data=auth, headers=headers, cookies=cookies)

authToken = r.json()['token']
headers['X-Auth-Token'] = authToken

r = requests.get('https://www.magicapp.org/api/v1/guidelines/2', headers=headers)

Feedback and Knowledge Base