Api.php 2.55 KB
Newer Older
Mindiell's avatar
Mindiell committed
1
2
3
4
5
6
<?php

/*
 * Static class for REST API communication
 */
class Api {
7
    // Generic get function for REST API
Mindiell's avatar
Mindiell committed
8
9
10
11
    static function get($url) {
        $curl = curl_init();
        $url = API_BASE . $url;
        curl_setopt($curl, CURLOPT_URL, $url);
12
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Mindiell's avatar
Mindiell committed
13
14
15
16
17
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

18
19
20
21
22
23
24
25
26
27
28
29
30
31
    // Generic post function for REST API
    static function post($url, $data) {
        $curl = curl_init();
        $url = API_BASE . $url;
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

    // Asking for campaign informations
Mindiell's avatar
Mindiell committed
32
33
34
35
36
    static function get_campaign() {
        $json = Api::get("campaign/" . CAMPAIGN_ID);
        return json_decode($json, true);
    }

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    // Searching a random or specific contact
    static function get_contact($contact_id='') {
        if ($contact_id!='') {
            $json = Api::get("campaign/" . CAMPAIGN_ID . "/contact/" . $contact_id);
        } else {
            $json = Api::get("campaign/" . CAMPAIGN_ID . "/contact");
        }
        return json_decode($json, true);
    }

    // Asking for complete list of contacts
    static function get_contacts() {
        $json = Api::get("campaign/" . CAMPAIGN_ID . "/contacts");
        return json_decode($json, true);
    }

    // Asking for complete list of groups
    static function get_groups() {
        $json = Api::get("campaign/" . CAMPAIGN_ID . "/groups");
        return json_decode($json, true);
    }

    // Asking for complete list of feedback categories
    static function get_feedback_categories() {
        $json = Api::get("campaign/" . CAMPAIGN_ID . "/feedback/categories");
        return json_decode($json, true);
    }

    // Asking for arguments by language
    static function get_arguments($language='en') {
        $json = Api::get("campaign/" . CAMPAIGN_ID . "/arguments/" . $language);
        return json_decode($json, true);
    }

    // Posting a feedback
    static function post_feedback($contact_id, $comment, $feedback_category='') {
        $data = array(
            "contact_id"=>$contact_id,
            "comment"=>$comment,
            "feedback_category"=>$feedback_category
        );
        $json = Api::post("campaign/" . CAMPAIGN_ID . "/feedback/add", $data);
        return json_decode($json, true);
    }

Mindiell's avatar
Mindiell committed
82
83
};