<?php
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 600);
// //error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';




// Use the REST API Client to make requests to the Twilio REST API
//use Twilio\Rest\Client;
$sid = "AC38af2d635db67338efd5cd106b9a2a28";
$token = "a84d4aebe8a63174c8f5300b26922570";

$twilio        = new Twilio\Rest\Client($sid, $token);
$accounts = $twilio->api->v2010->accounts->read();
$data_array=[];
           
foreach ($accounts as $record) {
 $sub_account_sid=$record->sid;
 $friendly_name=$record->friendlyName;
$total=0;
$curl = curl_init();
$api_url="https://api.twilio.com/2010-04-01/Accounts/".$sub_account_sid."/Usage/Records/ThisMonth.json?Category=totalprice";
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$sid:$token");
$response = curl_exec($curl);
$response=json_decode($response,true);
foreach($response as $res)
{
    if(is_array($res))
    {
        foreach($res as $re)
{
  $total = $re['price'];
  if($total>100)
  {
    
  $data_array[]=['friendlyName'=>$friendly_name,'sid'=>$sub_account_sid,'price'=>$total];
  }
  
  
}
    }


} 

}
// echo "<pre>";
// print_r($data_array);


$json = json_encode(array('data' => $data_array));
if (file_put_contents("twilio_data.json", $json))
    echo "JSON file created successfully...";
else 
    echo "Oops! Error creating json file...";
    error_log('cron job for twilio has been run');

// $array = Array (
//   "0" => Array (
//       "id" => "MMZ3aaaaaaaaaaaaaaaaaaaaaaaa01",
//       "name" => "Michael Bruce",
//       "designation" => "System Architect"
//   ),
//   "1" => Array (
//       "id" => "MMZ385",
//       "name" => "Jennifer Winters",
//       "designation" => "Senior Programmer"
//   ),
//   "2" => Array (
//       "id" => "MMZ593",
//       "name" => "Donna Fox",
//       "designation" => "Office Manager"
//   )
// );

// // encode array to json
// $json = json_encode(array('data' => $array));

// //write json to file
// if (file_put_contents("twilio_data.json", $json))
//   echo "JSON file created successfully...";
// else 
//   echo "Oops! Error creating json file...";





?>