backend.php (1424B)
1 <?php 2 // This file is in the public domain. 3 4 include_once 'config.php'; 5 include_once 'helpers.php'; 6 7 /** 8 * 'body' is an object, representing the JSON to POST. NOTE: we do NOT 9 * rely on a more structured way of doing HTTP, like the one offered by 10 * pecl_http, as its installation was NOT always straightforward. 11 */ 12 function post_to_backend($backend_uri, $body){ 13 $json = json_encode($body); 14 $c = curl_init(url_join ($GLOBALS['BACKEND'], $backend_uri)); 15 $options = array(CURLOPT_RETURNTRANSFER => true, 16 CURLOPT_CUSTOMREQUEST => "POST", 17 CURLOPT_POSTFIELDS => $json, 18 CURLOPT_HTTPHEADER => 19 array('Content-Type: application/json')); 20 curl_setopt_array($c, $options); 21 $r = curl_exec($c); 22 return array("status_code" => curl_getinfo($c, CURLINFO_HTTP_CODE), 23 "body" => $r); 24 } 25 26 /** 27 * Sends a GET request to the backend. 28 */ 29 function get_to_backend($backend_url, $args){ 30 $path = sprintf("%s?%s", $backend_url, http_build_query($args)); 31 $c = curl_init(url_join($GLOBALS['BACKEND'], $path)); 32 33 $options = array(CURLOPT_RETURNTRANSFER => true, 34 CURLOPT_CUSTOMREQUEST => "GET"); 35 curl_setopt_array($c, $options); 36 $r = curl_exec($c); 37 return array("status_code" => curl_getinfo($c, CURLINFO_HTTP_CODE), 38 "body" => $r); 39 } 40 ?>