Using My Access Token

Using your Access Token to query the Datacenter's RESTful API is pretty straight forward and even easier to test using the RESTful API tool, Insomnia.

The Datacenter's API uses Authentication of type BearerToken and requires a Multipart Form when submitting data with your POST Request, such as a brochure's id or an article's id.

In the code example on the right we first setup a function called 'queryapi' that we can used again and again and simply parse the API's endpoint url and any POST data to it. In the 'queryapi' function we accept the endpoint as a string and $post variable as an 'Array'. We then check to see if the $post variable is false or has data and using PHP's curl functionality we submit the $post data on a POST Request if it exsits as an Array, otherwise we submit the $post variable as 'false' on a GET Request. The two requests are setup slightly differently depending on which request type is being called.

The function 'get_all_brochures' is a GET Request example using the resuable 'queryapi' function and simply parses the endpoint as a string and the $post variable as false.

Please feel free to copy and use the example code.

Below the example code is the result output JSON format that you will receive on successful authentication and request.

BEARER TOKEN USAGE EXAMPLE

/* create a reusable function */
public function queryapi($endpoint, $post = false){
    
    $return = false;
    
    $access_token = "your_access_token";
    
    if($access_token){
        // set bearer token string.
        $authorization = "Authorization: Bearer ".$access_token;
        // start curl setup.
        $ch = curl_init($endpoint);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // check if $post variables have been submitted with request.
        if($post){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            //send array variable.
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            //set content type as multipart/form-data for posting data to api.
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', $authorization));
        } else {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
        }

        // execute curl setup.
        $return = curl_exec($ch);
        // close curl connection.
        curl_close($ch);

    } else {
        $return = false;
    }

    return $return;
}

/* create a reusable function */
public function get_all_brochures(){
    $results = $this->queryapi('https://datacenter.medinformer.co.za/api/brochures', false);
    return $results;
}

$json = $this->get_all_brochures();
echo print_r($json, true);



[
    {
        "id": 239,
        "title": "Post-Traumatic Stress Disorder (PTSD)",
        "desc": "PTSD is a serious and often debilitating medical condition that can occur in anyone who has experienced a traumatic event",
        "categories": [
            "Central Nervous System (CNS)",
            "Mental Health"
        ],
        "image": "https:\/\/datacenter.medinformer.co.za\/images\/brochures\/post-traumaticstressdisorderptsd.jpg",
        "published": 1,
        "created_at": "2018-12-13 11:57:25",
        "updated_at": "2019-06-13 10:53:33"
    },
    { ... }
    { ... }
    { ... }
    ...
]