Let the platform do the work

Creating or Updating Teams

Overview

A PHP example demonstrating how to manipulate teams using cURL and the v4_1 REST API.

Note: If you are creating a private team for a user, you will need to set private to true and populate the associated_user_id populated. You should also populate the name and name_2 properties with the users first and last name.

Example

<?php

    $url = "http://{site_url}/service/v4_1/rest.php";
    $username = "admin";
    $password = "password";

    //function to make cURL request
    function call($method, $parameters, $url)
    {
        ob_start();
        $curl_request = curl_init();
   
        curl_setopt($curl_request, CURLOPT_URL, $url);
        curl_setopt($curl_request, CURLOPT_POST, 1);
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, 1);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
   
        $jsonEncodedData = json_encode($parameters);
   
        $post = array(
            "method" => $method,
            "input_type" => "JSON",
            "response_type" => "JSON",
            "rest_data" => $jsonEncodedData
        );
   
        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($curl_request);
        curl_close($curl_request);
   
        $result = explode("\r\n\r\n", $result, 2);
        $response = json_decode($result[1]);
        ob_end_flush();
   
        return $response;
    }
   
    //login --------------------------------------------
   
    $login_parameters = array(
        "user_auth" => array(
            "user_name" => $username,
            "password" => md5($password),
            "version" => "1"
        ),
        "application_name" => "RestTest",
        "name_value_list" => array(),
    );
   
    $login_result = call("login", $login_parameters, $url);
   
    /*
    echo "<pre>";
    print_r($login_result);
    echo "</pre>";
    */
   
    //get session id
    $session_id = $login_result->id;
   
    //create team -----------------------------------------------
   
    $set_entry_parameters = array(
   
        // session id
        "session" => $session_id,
   
        // The name of the module that the record will be create in.
        "module_name" => "Teams",
   
        // array of arrays for the record attributes
        "name_value_list" => array(
            /* Setting the id with a valid record id will update the record.
            array(
                "name" => "id",
                "value" => "47dbab1d-bd78-09e8-4392-5256b4501d90"
            ),
            */
            array(
                "name" => "name",
                "value" => "My Team"
            ),
            array(
                "name" => "description",
                "value" => "My new team"
            ),
            //Whether the team is private.
            //Private teams will have the associated_user_id populated.
            array(
                "name" => "private",
                "value" => 0
            ),
        ),
    );
   
    $set_entry_result = call("set_entry", $set_entry_parameters, $url);
   
    echo "<pre>";
    print_r($set_entry_result);
    echo "</pre>";

?>

Result

stdClass Object
(
    [id] => 5c35a3be-4601-fb45-3afd-52ab78b03f89
    [entry_list] => stdClass Object
        (
            [name] => stdClass Object
                (
                    [name] => name
                    [value] => My Team
                )

            [description] => stdClass Object
                (
                    [name] => description
                    [value] => My new team
                )

            [private] => stdClass Object
                (
                    [name] => private
                    [value] =>
                )

        )

)