TeamWork API cURL function

TeamWork API cURL function

Just like the Bitbucket and SupportBee functions I’ve posted recently here is a quick, simple php function that might help you access the TeamWork API. Again it is very basic but if you use it with the other functions you can build out a nice dashboard of your active projects.

function TeamworkGet($action, $options = array()) {
        // Define company/organization TeamworkPM URL.
        define('TW_API_URL', %YOUR URL%);
        // Define the API Token of the user.
        define('TW_API_TOKEN', %YOUR TOKEN%);
        $curl = curl_init();

        // Convert options array into param list.
        $params = empty($options) ? '' : '?' . http_build_query($options);
        // Set curl options
        curl_setopt($curl, CURLOPT_URL, TW_API_URL . $action . '.json' . $params);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
                array('Authorization: BASIC ' . base64_encode(TW_API_TOKEN . ':xxx'))
        );

        // Run the HTTP request and decode the result.
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);

}

Simple and easy to use, this function might come in handy if you need to access the TeamWork API