summaryrefslogtreecommitdiff
path: root/php/backend.php
blob: a0cce8317b0eef10b898b41df60ba131363fec86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
  // This file is in the public domain.

  include_once 'config.php';
  include_once 'helpers.php';

  /**
   * 'body' is an object, representing the JSON to POST. NOTE: we do NOT
   * rely on a more structured way of doing HTTP, like the one offered by
   * pecl_http, as its installation was NOT always straightforward.
   */
  function post_to_backend($backend_uri, $body){
    $json = json_encode($body);
    $c = curl_init(url_join ($GLOBALS['BACKEND'], $backend_uri));
    $options = array(CURLOPT_RETURNTRANSFER => true,
                     CURLOPT_CUSTOMREQUEST => "POST",
                     CURLOPT_POSTFIELDS => $json,
                     CURLOPT_HTTPHEADER =>
                       array('Content-Type: application/json'));
    curl_setopt_array($c, $options);
    $r = curl_exec($c);
    return array("status_code" => curl_getinfo($c, CURLINFO_HTTP_CODE),
                 "body" => $r);
  }

  /**
   * Sends a GET request to the backend.
   */
  function get_to_backend($backend_url, $args){
    $path = sprintf("%s?%s", $backend_url, http_build_query($args));
    $c = curl_init(url_join($GLOBALS['BACKEND'], $path));

    $options = array(CURLOPT_RETURNTRANSFER => true,
                     CURLOPT_CUSTOMREQUEST => "GET");
    curl_setopt_array($c, $options);
    $r = curl_exec($c);
    file_put_contents("/tmp/php.out", print_r($r, true));
    return array("status_code" => curl_getinfo($c, CURLINFO_HTTP_CODE),
                 "body" => $r);
  }


?>