Api.php 2.3 KB
Newer Older
1 2 3 4 5 6
<?php

/*
 * Static class for REST API communication
 */
class Api {
7
    // Generic get function for REST API
8 9
    static function get($url) {
        $curl = curl_init();
10
        $url = API_BASE . $url . "/?format=json";
11
        curl_setopt($curl, CURLOPT_URL, $url);
12
        //curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
13
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
14 15 16 17 18
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

19 20 21 22 23 24 25 26 27 28 29 30 31 32
    // 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
33
    static function get_campaign() {
34
        $json = Api::get("campaigns/" . CAMPAIGN_ID);
35 36 37 38 39
        return json_decode($json, true);
    }

    // Asking for complete list of contacts
    static function get_contacts() {
40
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/contacts");
41 42 43 44 45
        return json_decode($json, true);
    }

    // Asking for complete list of groups
    static function get_groups() {
46
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/groups");
47 48 49 50 51
        return json_decode($json, true);
    }

    // Asking for complete list of feedback categories
    static function get_feedback_categories() {
52
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/feedback/categories");
53 54 55 56 57
        return json_decode($json, true);
    }

    // Asking for arguments by language
    static function get_arguments($language='en') {
58
        $json = Api::get("campaigns/" . CAMPAIGN_ID . "/arguments");
59 60 61 62 63 64 65 66 67 68
        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
        );
69
        $json = Api::post("campaigns/" . CAMPAIGN_ID . "/feedback/add", $data);
70 71 72
        return json_decode($json, true);
    }

73 74
};