Let the platform do the work

Retrieving a List of Records With Related Info

Overview

A PHP example demonstrating how to retrieve a list of records with info from a related entity with the get_entry_list method using cURL and the v4_1 REST API.

This example will retrieve a list of contacts and their related email addresses.

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;

    //retrieve records ------------------------------------- 

    $get_entry_list_parameters = array(

         //session id
         'session' => $session_id,

         //The name of the module from which to retrieve records
         'module_name' => "Contacts",

         //The SQL WHERE clause without the word "where".
         'query' => "",

         //The SQL ORDER BY clause without the phrase "order by".
         'order_by' => "",

         //The record offset from which to start.
         'offset' => "0",

         //Optional. The list of fields to be returned in the results
         'select_fields' => array(
              'id',
              'first_name',
              'last_name',
         ),

         //A list of link_names and for each link_name, what fields value to be returned. 
        // For ex.'link_name_to_fields_array' => array(array('name' =>  'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
         'link_name_to_fields_array' => array(
              array(
                   'name' => 'email_addresses',
                   'value' => array(
                        'id',
                        'email_address',
                        'opt_out',
                        'primary_address'
                   ),
              ),
         ),

         //The maximum number of results to return.
         'max_results' => '2',

         //To exclude deleted records
         'deleted' => 0,

         //If only records marked as favorites should be returned.
         'Favorites' => false,

    );

    $get_entry_list_result = call("get_entry_list", $get_entry_list_parameters, $url);

    echo "<pre>";
    print_r($get_entry_list_result);
    echo "</pre>";

?>

Result

  stdClass Object
(
    [result_count] =&gt; 2
    [total_count] =&gt; 200
    [next_offset] =&gt; 2
    [entry_list] =&gt; Array
        (
            [0] =&gt; stdClass Object
                (
                    [id] =&gt; 116d9bc6-4a24-b826-952e-4f7cb6b25ea7
                    [module_name] =&gt; Contacts
                    [name_value_list] =&gt; stdClass Object
                        (
                            [id] =&gt; stdClass Object
                                (
                                    [name] =&gt; id
                                    [value] =&gt; 116d9bc6-4a24-b826-952e-4f7cb6b25ea7
                                )

                            [first_name] =&gt; stdClass Object
                                (
                                    [name] =&gt; first_name
                                    [value] =&gt; Lucinda
                                )

                            [last_name] =&gt; stdClass Object
                                (
                                    [name] =&gt; last_name
                                    [value] =&gt; Jacoby
                                )

                        )

                )

            [1] =&gt; stdClass Object
                (
                    [id] =&gt; 11c263ef-ff61-71ff-f090-4f7cb6fc9f68
                    [module_name] =&gt; Contacts
                    [name_value_list] =&gt; stdClass Object
                        (
                            [id] =&gt; stdClass Object
                                (
                                    [name] =&gt; id
                                    [value] =&gt; 11c263ef-ff61-71ff-f090-4f7cb6fc9f68
                                )

                            [first_name] =&gt; stdClass Object
                                (
                                    [name] =&gt; first_name
                                    [value] =&gt; Ike
                                )

                            [last_name] =&gt; stdClass Object
                                (
                                    [name] =&gt; last_name
                                    [value] =&gt; Gassaway
                                )

                        )

                )

        )

    [relationship_list] =&gt; Array
        (
            [0] =&gt; stdClass Object
                (
                    [link_list] =&gt; Array
                        (
                            [0] =&gt; stdClass Object
                                (
                                    [name] =&gt; email_addresses
                                    [records] =&gt; Array
                                        (
                                            [0] =&gt; stdClass Object
                                                (
                                                    [link_value] =&gt; stdClass Object
                                                        (
                                                            [id] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; id
                                                                    [value] =&gt; 13066f13-d6ea-405c-0f95-4f7cb6fa3a08
                                                                )

                                                            [email_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; email_address
                                                                    [value] =&gt; support52@example.org
                                                                )

                                                            [opt_out] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; opt_out
                                                                    [value] =&gt; 0
                                                                )

                                                            [primary_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; primary_address
                                                                    [value] =&gt; 
                                                                )

                                                        )

                                                )

                                            [1] =&gt; stdClass Object
                                                (
                                                    [link_value] =&gt; stdClass Object
                                                        (
                                                            [id] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; id
                                                                    [value] =&gt; 13e3d111-b226-c363-4832-4f7cb699a3a0
                                                                )

                                                            [email_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; email_address
                                                                    [value] =&gt; qa.section@example.it
                                                                )

                                                            [opt_out] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; opt_out
                                                                    [value] =&gt; 1
                                                                )

                                                            [primary_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; primary_address
                                                                    [value] =&gt; 
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

            [1] =&gt; stdClass Object
                (
                    [link_list] =&gt; Array
                        (
                            [0] =&gt; stdClass Object
                                (
                                    [name] =&gt; email_addresses
                                    [records] =&gt; Array
                                        (
                                            [0] =&gt; stdClass Object
                                                (
                                                    [link_value] =&gt; stdClass Object
                                                        (
                                                            [id] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; id
                                                                    [value] =&gt; 16aeaf8b-b31f-943d-3844-4f7cb6ac63f2
                                                                )

                                                            [email_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; email_address
                                                                    [value] =&gt; vegan.sales.im@example.cn
                                                                )

                                                            [opt_out] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; opt_out
                                                                    [value] =&gt; 0
                                                                )

                                                            [primary_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; primary_address
                                                                    [value] =&gt; 
                                                                )

                                                        )

                                                )

                                            [1] =&gt; stdClass Object
                                                (
                                                    [link_value] =&gt; stdClass Object
                                                        (
                                                            [id] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; id
                                                                    [value] =&gt; 173bacf5-5ea6-3906-d91d-4f7cb6c6e883
                                                                )

                                                            [email_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; email_address
                                                                    [value] =&gt; kid.the.section@example.org
                                                                )

                                                            [opt_out] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; opt_out
                                                                    [value] =&gt; 1
                                                                )

                                                            [primary_address] =&gt; stdClass Object
                                                                (
                                                                    [name] =&gt; primary_address
                                                                    [value] =&gt; 
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Topics