Friday, 26 July 2013

Retrieving a List of Fields From a Module - REST and PHP

Overview

A PHP example demonstrating how to retrieve fields vardefs from the accounts module with the get_module_fields method using cURL and the v4 REST API.
This example will only retrieve the vardefs for the 'id' and 'name' fields.

Example

01<?php
02 
03    $url = "http://{site_url}/service/v4/rest.php";
04    $username = "admin";
05    $password = "password";
06 
07    function call($method, $parameters, $url)
08    {
09        ob_start();
10        $curl_request = curl_init();
11 
12        curl_setopt($curl_request, CURLOPT_URL, $url);
13        curl_setopt($curl_request, CURLOPT_POST, 1);
14        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
15        curl_setopt($curl_request, CURLOPT_HEADER, 1);
16        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
17        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
18        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
19 
20        $jsonEncodedData = json_encode($parameters);
21 
22        $post = array(
23             "method" => $method,
24             "input_type" => "JSON",
25             "response_type" => "JSON",
26             "rest_data" => $jsonEncodedData
27        );
28 
29        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
30        $result = curl_exec($curl_request);
31        curl_close($curl_request);
32 
33        $result = explode("\r\n\r\n", $result, 2);
34        $response = json_decode($result[1]);
35        ob_end_flush();
36 
37        return $response;
38    }
39 
40    //login -------------------------------------------
41 
42    $login_parameters = array(
43         "user_auth"=>array(
44              "user_name"=>$username,
45              "password"=>md5($password),
46              "version"=>"1"
47         ),
48         "application_name"=>"RestTest",
49         "name_value_list"=>array(),
50    );
51 
52    $login_result = call("login", $login_parameters, $url);
53 
54    /*
55    echo "<pre>";
56    print_r($login_result);
57    echo "</pre>";
58    */
59 
60    //get session id
61    $session_id = $login_result->id;
62 
63    //retrieve fields -----------------------------------
64 
65    $get_module_fields_parameters = array(
66 
67         //session id
68         'session' => $session_id,
69 
70         //The name of the module from which to retrieve records
71         'module_name' => 'Accounts',
72 
73         //Optional. Returns vardefs for the specified fields. An empty array will return all fields.
74         'fields' => array(
75             'id',
76             'name',
77         ),
78    );
79 
80    $get_module_fields_result = call("get_module_fields", $get_module_fields_parameters, $url);
81 
82    echo "<pre>";
83    print_r($get_module_fields_result);
84    echo "</pre>";
85 
86?>

Result

01stdClass Object
02(
03    [module_name] => Accounts
04    [table_name] => accounts
05    [module_fields] => stdClass Object
06        (
07            [id] => stdClass Object
08                (
09                    [name] => id
10                    [type] => id
11                    [group] =>
12                    [id_name] =>
13                    [label] => ID
14                    [required] => 1
15                    [options] => Array
16                        (
17                        )
18 
19                    [related_module] =>
20                    [calculated] =>
21                    [len] =>
22                )
23 
24            [name] => stdClass Object
25                (
26                    [name] => name
27                    [type] => name
28                    [group] =>
29                    [id_name] =>
30                    [label] => Name:
31                    [required] => 1
32                    [options] => Array
33                        (
34                        )
35 
36                    [related_module] =>
37                    [calculated] =>
38                    [len] => 150
39                )
40 
41        )
42 
43    [link_fields] => Array
44        (
45        )
46 
47)

1 comment:

  1. Retrieving a List of Fields From a Module - REST and PHP

    ReplyDelete