DHIS2 Data Export Using PHP cURL Technology

In this tutorial, we will explain how to export data elements values from DHIS2 by using PHP cURL Technology:

Business Scenario:

  • 1. If you want to make an external dashboard by using DHIS2 data, you can use this technology
  • 2. If you want to analysis raw data of DHIS2 in another external system, you may use this script.
  • 3. You can export Organization Units, Data Sets, Data Element Groups, Data Elements and Data Value Set.
  • 4. You can bridge two different systems by using this script.
  • 5. Schedule based data export from DHIS2

Necessary Requirements:

  • 1. Apache Server
  • 2. MySQL or any other Database
  • 3. DHIS2 User Authentication, Access Key, Data Set, Data Elements
  • Step-1: Local Database Configuration
  • Step-2, 3: User Interface and jQuery Script
  • Step-4: Organization Unit and Elements making as an array
  • Step-5: PHP Script by using DHIS2 Web API link
  • Step-6: Schedule Writing in Control Panel
  • Step-7: Final Result
  • Step-1: Database, Table and Connection creation
In this step you need to create Local Database, table and ensure your database connection:

Step-1.1 Database Creation: Create database in phpMyAdmin as:


Step-1.2 Table Creation: Create a table that will store DHIS2 exported data as:


Step-1.3 Create Local Database Connection:



class dbConnect{

protected $product_details_info = array();

public $mysqli;

public $data;
  • Create DB Connection public function __construct(){
  $this->mysqli = new mysqli('localhost','root','','elmis'); if(mysqli_connect_errno()) {   echo "Error: Could not connect to database."; exit;   } else{ //echo"Your Database successfully connected"; } } public function __destruct(){ $this->mysqli->close(); }   }   ?>
Step-2: Create the User Interface as:
  • DHIS2 Username,
  • Password,
  • Web Link (DHIS2 Application URL),
  • DataSet,
  • Periods,
  • Organization Units(Optional)

<select class="form-control" name="uname" id="uname" > <option name="">User Name</option>


<option name=" Username1">Username1</option> <option name=" Username2"> Username2</option> <option name=" Username3"> Username3</option> </select>


<input type="password" name="password" class="form-control" id="password"> <select class="form-control" name="webLink" id="webLink" >

<option name="">Select cURL</option>


<option value="Your Server Link">Central Server</option> <option value=" Your Server Link Development Server</option> </select>


<select class="form-control" name="dataset" id="dataset" > <option name="">Select Data Set</option>


<option value="iFFVg2xgFPL">Monthly eLMIS DataSet-Central</option> <option value="ACzf2x2bF">Monthly eLMIS DataSet-Development</option> </select>


<select class="form-control" name="period" id="period" > <option name="">Select Month</option> <option value="201501">January</option>

<option value="201502">February</option>

<option value="201503">March</option>

<option value="201504">April</option>

<option value="201505">May</option>

<option value="201506">June</option>

<option value="201507">July</option>

<option value="201508">August</option>

<option value="201509">September</option>

<option value="201510">October</option>

<option value="201511">November</option>

<option value="201512">December</option>


<input type="text" name="orgUnit" class="form-control" id="orgUnit">


<button type="submit" class="btn btn-success" id="dataupload_button">Export eLMIS Data</button>


Step-3: Paste the jQuery POST script bellow your HTML for Data Request submit as:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script>



$('#dataupload_form').submit(function (e) {



var uname = $('#uname').val();

var password= $('#password').val();

var webLink = $('#webLink').val();

var dataset = $('#dataset').val();


var period = $('#period').val();


$('#dataupload_button').after('<div class="loader" style="position: fixed;left: 50%;top: 40%;width: 100%;height: 100%;z-index: 9999;"><img src="assets/images/load.gif" alt="Importing eLMIS Data" /></div>');


{uname:uname,password:password,webLink:webLink,dataset:dataset,period:period },













Design of Step-1 and Step-2:
dhis-2-4 Step-4: Organization Unit and Data Elements:
  Step-4.1 Create Organization Unit as an array:




'Gazipur District Hospital, Gazipur'=>'gWWPtZ3ea8b', 'Gazipur Civil Surgeons Office, Gazipur'=>'dkBQYOr7tYN', 'Gazipur Sadar UHC'=>'ZycDiLBirIF',

'Kaliakair UHC'=>'hkL28MU5Qv5',

'Kaliganj GZ UHC'=>'zRBzPCrNByW',




Step-4.2 Create Exported Data Elements as an Array:




'ta8v9gj6aQC' => 'Inj.Ampicillin2_OpeningBalance', 'yQfHIwxvCmb' => 'Inj.Ampicillin2_ReceiptsThisMonth', 'CRueZPFX52M' => 'Inj.Ampicillin2_AdjustmentPlus', 'jsXI01cQX6S' => 'Inj.Ampicillin2_AdjustmentMinus', 'O2Nu7efJ5CB' => 'Inj.Ampicillin2_Consumption', 'GuO5I4Vusdk' => 'Inj.Ampicillin2_ClosingBalance', 'VsS1R8DztGn' => 'Inj.Ampicillin2_NumofDaysStockOut', 'pnBJrRFysDY' => 'Inj.Ampicillin2_Comments',



Step-5: Write PHP Script using DHIS2 Web API

  • Include Your database include'database.php';
  class dataValueLoad extends dbConnect{ public function elmisDataSetValues($username,$password,$webLink,$dataset,$period){  
  • Include your expected organization units and data elements
include 'org-unit.php'; include 'medicins_name.php'; foreach($org as $orgUnitName=>$orgId){ // DHIS2 Web API setting   $url =$webLink."/api/dataValueSets?dataSet=$dataset&period=$period&orgUnit=$orgId"; // cURL Initialization and execution   $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code $result=curl_exec ($ch); curl_close ($ch); $data['resultData']= json_decode($result, true);   $i=0;   global $element_value; $dataset_id=1001; if($i<=7){ foreach($data as $val){ foreach($resultDataElements as $element_idArray=>$element_name){ $orgUnitServer =$data['resultData']["dataValues"][$i]["orgUnit"]; $element_idServer=$data['resultData']["dataValues"][$i]["dataElement"];  
  • if($element_idArray==$element_idServer && $orgId=$orgUnitServer){
  • Exported Elements value storing
$element_value =$data['resultData']["dataValues"][$i]["value"]; if(isset($element_value) && $element_value!=0){ $storedBy=$data['resultData']["dataValues"][$i]["storedBy"]; $created=$data['resultData']["dataValues"][$i]["created"]; $lastUpdated=$data['resultData']["dataValues"][$i]["lastUpdated"]; // Insert exported Data in Local Database $query="INSERT INTO dataset_values SET   dataset_id='$dataset_id',period='$period',orgunit='$orgId',orgname='$orgUnitName',element_id='$ele ment_idServer',element_value='$element_value',stored_by='$storedBy',created_date='$created',last_u pdate='$lastUpdated'";   $result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Data cannot inserted.DB Installation data already existed. "); if($result){ //echo 'Data Successfully Inserted.'; } //} } $dataset_id++; $i++; } } }else{ die(); } } } } $username=$_POST['uname']; $password=$_POST['password']; $webLink=$_POST['webLink']; $dataset=$_POST['dataset']; $period=$_POST['period'];//$period="201507"; //07 for July// $period=$_POST['period']; $clsAccess=new dataValueLoad(); $clsAccess->elmisDataSetValues($username,$password,$webLink,$dataset,$period);   ?>  
Step-6: Host this script in control panel and add schedule as:

Step-7: Run this Script by using your DHIS2 Credentials and check the local database.
Final Output: