Example #1
0
echo "\n<h2>Meta data look up array</h2>";
$temp = "<pre>";
$temp .= print_r($metadata_lookup, 1);
$temp .= "</pre>";
echo $temp;
echo "<hr/>\n";
//print the data to the screen
//echo $metadata_xml;
///////////////////////////////////////////////////////
//Records
//This is the actual patient data
///////////////////////////////////////////////////////
//The rawOrLabel flag does the look up for me to get the real data out.
$options = array('content' => 'record', 'type' => 'flat', 'format' => 'xml', 'rawOrLabel' => 'label', 'token' => $token);
//create a new API request object
$request = new RestCallRequest("localhost/api/", 'POST', $options);
//initiate the API request
$request->execute();
$data_xml = $request->getResponseBody();
//By getting rid of the blank XML elements here we dont have to deal with
//empty fields that haven't been filled in due to branching logic
//excluding them.
$data = simplexml_load_string($data_xml, 'SimpleXMLElement', LIBXML_NOBLANKS);
//////////////////////////////////////////////////////////////////
//Ideally we would have sanitized all the data by now and made
//sure all the required fields are present.
$number_of_patients = count($data->item);
echo $number_of_patients . " patients to process<br/>\n";
//Cycle through each set of forms.
//A key point here is that I pick out the fields that I expect to be there for
//non-observations, use them, then unset them. Everything that is then left
Example #2
0
<?php

//Add a new entry to REDCap for this API token.
//27/3/2012
//Olly Butters
//the class that performs the API call
require_once 'RestCallRequest.php';
//Collection of API tokens needed to get data from REDCap
$token = "FD72BD71353EE1ABAAD4B641BA8245F5";
//olly/BRISSkit sample/eduserv
$new_participant = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><records><item><brisskit_id>123</brisskit_id></item></records>";
$data = array('content' => 'record', 'type' => 'flat', 'format' => 'xml', 'token' => $token, 'data' => $new_participant);
# create a new API request object
$request = new RestCallRequest("localhost/redcap/api/", 'POST', $data);
# initiate the API request
$request->execute();
# the following line will print out the entire HTTP request object
# good for testing purposes to see what is sent back by the API and for debugging
echo '<pre>' . print_r($request, true) . '</pre>';
# print the output from the API
echo $request->getResponseBody();
Example #3
0
 public function api_get_string($data, $error)
 {
     $data['token'] = $this->token;
     // Create REDCap API request object
     $request = new RestCallRequest($this->api_url, $data);
     // Initiate the API request, and fetch the results from the request object.
     $request->execute();
     $body_str = $request->getResponseBody();
     $header_array = $request->getResponseInfo();
     // Decode the JSON content returned by REDCap (into an array rather
     // than into an object by specifying the second argument as "true"),
     $body_array = json_decode($body_str, true);
     // If an error is returned, there will be an 'error' key in
     // the $body_array
     if (is_array($body_array) && array_key_exists('error', $body_array)) {
         $error .= "Error: '" . $body_array['error'] . "'\n";
         $this->notifier->notify($error);
         return false;
     } else {
         return $body_str;
     }
 }
# the class that performs the API call
echo "hey 1 <br />";
require_once 'RestCallRequest.php';
echo "hey 2 <br />";
# arrays to contain elements you want to filter results by
# example: array('item1', 'item2', 'item3');
$records = array();
$events = array();
$fields = array();
$forms = array();
echo "hey 3 <br />";
# an array containing all the elements that must be submitted to the API
$data = array('content' => 'record', 'type' => 'flat', 'format' => 'csv', 'records' => $records, 'events' => $events, 'fields' => $fields, 'forms' => $forms, 'token' => '19BE63523DB731FA55CD2E39C267E4B0');
echo "hey 4 <br />";
# create a new API request object
$request = new RestCallRequest("API_URL", 'POST', $data);
echo "hey 5 <br />";
# initiate the API request
$request->execute();
echo "hey 6 <br />";
# get the content type of the data being returned
$response = $request->getResponseInfo();
$type = explode(";", $response['content_type']);
$contentType = $type[0];
# set the content type of page
header("Content-type: {$contentType}; charset=utf-8");
#print the data to the screen
echo $request->getResponseBody();
# the following line will print out the entire HTTP request object
# good for testing purposes to see what is sent back by the API and for debugging
echo '<pre>' . print_r($request, true) . '</pre>';
Example #5
0
$fields = array();
$forms = array();
# an array containing all the elements that must be submitted to the API
$data = array('content' => 'record', 'type' => 'flat', 'format' => 'csv', 'records' => $records, 'events' => $events, 'fields' => $fields, 'token' => $local_token);
# create a new API request object for the local machine
$request_local = new RestCallRequest($init_params['local_ip'], 'POST', $data);
# initiate the API request for the data on the local machine
$request_local->execute();
#$local_response = $request_local->getResponseInfo();
$exportdata = $request_local->getResponseBody();
#data from the local machine
//print_r($exportdata);
# an array containing all the elements that must be submitted to the server API
$data = array('content' => 'record', 'type' => 'flat', 'format' => 'csv', 'token' => $server_token, 'data' => $exportdata);
# create a new API request object for the server
$request_server = new RestCallRequest($init_params['server_ip'], 'POST', $data);
# initiate the API request to the server
$request_server->execute();
$server_response = $request_server->getResponseInfo();
//print_r($server_response);
$log_message = implode("\n", array_map(function ($v, $k) {
    return $k . '=' . $v;
}, $server_response, array_keys($server_response)));
# Prepare log folder
$path = $init_params['log_location'];
$response_code = $server_response['http_code'];
if ($response_code != '200') {
    //there was an error
    echo json_encode(array("<h2>ERROR!</h2><br/>", return_httpstat($response_code)));
} else {
    //Success message here