Logging In - REST and PHP

The following script renders the table of contents for this page.

Overview

Use the section below to provide a short description of the technology. This section is used by other pages to provide an abstract of this page.

A PHP example demonstrating how to log in and retrieve a session key using cURL and the v4 REST API.

Example

<?php

    $url = "http://{site_url}/service/v4/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;

?>

Result

stdClass Object
(
    [id] => lb7479svj8pjtf57ipmshepo80
    [module_name] => Users
    [name_value_list] => stdClass Object
        (
            [user_id] => stdClass Object
                (
                    [name] => user_id
                    [value] => 1
                )

            [user_name] => stdClass Object
                (
                    [name] => user_name
                    [value] => admin
                )

            [user_language] => stdClass Object
                (
                    [name] => user_language
                    [value] => en_us
                )

            [user_currency_id] => stdClass Object
                (
                    [name] => user_currency_id
                    [value] => -99
                )

            [user_is_admin] => stdClass Object
                (
                    [name] => user_is_admin
                    [value] => 1
                )

            [user_default_team_id] => stdClass Object
                (
                    [name] => user_default_team_id
                    [value] => 1
                )

            [user_default_dateformat] => stdClass Object
                (
                    [name] => user_default_dateformat
                    [value] => m/d/Y
                )

            [user_default_timeformat] => stdClass Object
                (
                    [name] => user_default_timeformat
                    [value] => h:ia
                )

            [user_number_seperator] => stdClass Object
                (
                    [name] => user_number_seperator
                    [value] => ,
                )

            [user_decimal_seperator] => stdClass Object
                (
                    [name] => user_decimal_seperator
                    [value] => .
                )

            [mobile_max_list_entries] => stdClass Object
                (
                    [name] => mobile_max_list_entries
                    [value] => 10
                )

            [mobile_max_subpanel_entries] => stdClass Object
                (
                    [name] => mobile_max_subpanel_entries
                    [value] => 3
                )

            [user_currency_name] => stdClass Object
                (
                    [name] => user_currency_name
                    [value] => US Dollars
                )

        )

)

JerryC.png

This section automatically lists related pages, such as child pages or pages related by tags. Remove this section if you do not want to show related pages.