<?php

function results($data) {
    $url = 'https://www.tesults.com/results';
    $ch = curl_init($url);
    $jsonDataEncoded = json_encode($data);
    if(!is_string($jsonDataEncoded)){
        return array("success" => false, "message" => "Error with data, check you are passing in an associative array.");
    }
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
    if(!is_array($decoded)){
        return array("success" => false, "message" => "Response error, check you are passing in an associative array.");
    }
    
    // Process response.
    $success = false;
    $message = "";

    if (array_key_exists("data", $decoded)) {
        $success = true;
        $message = $decoded["data"]["message"];
    } else {
        $success = false;
        $message = $decoded["error"]["message"];
    }

    return array("success" => $success, "message" => $message);
}

?>