How to get Toky call data records

πŸ“˜

API Endpoint used

/cdrs

Parameters you need to replace in the getCDRsByDate function:

Parameters for the sample:

  • [[DATE1]]: Initial date for the call data records.
  • [[DATE2]]: Final date range.
  • [[PAGESIZE]]: Number of records per page loaded. Maximum 500 records.

Date format examples: 2020-01-01 or 2020-01-01 00:00:00

function getCDRsByDate($dateIni, $dateEnd)
{
    //example with two dates filter
    // create a new cURL resource
    $ch = curl_init();
    $api_key = '[[TOKY_API_KEY]]';
    $pageRows =[[PAGESIZE]]; //The maximum limit of CDR rows that will come from the request. Max value 500.
    $headers = array();
    $headers[] = "X-Toky-Key: {$api_key}";
	$CDRData[]="";
    //filter format (created_at >= 2017-06-01) AND (created_at <= 2017-06-02)
    $filter_enc = urlencode("(created_at >= $dateIni) AND (created_at <= $dateEnd)");
    // set URL and other appropriate options&
    curl_setopt($ch, CURLOPT_URL, "https://api.toky.co/v1/cdrs?limit=$pageRows&filter=$filter_enc");
    echo "https://api.toky.co/v1/cdrs?limit=$pageRows&filter=$filter_enc";
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($ch); // Send request
    curl_close($ch); // close cURL resource 
    $result = json_decode($curl_response,true);
    if(sizeOf($result["results"])>0){
        $CDRData = array_merge($CDRData,$result["results"]);
    }
    while (!is_null ($result["next"])){ //If"next" is null is because you are in the last page of data
        sleep(2); // A small pause after the next request
        $ch = curl_init();		
        //Use the returned "next" field to get the next CDRs data page. This returns a link and you just have to execute the request.
        // set URL and other appropriate options&
        curl_setopt($ch, CURLOPT_URL, $result["next"]);
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
        $curl_response = curl_exec($ch); // Send request
        curl_close($ch); // close cURL resource 
        $result = json_decode($curl_response,true);
        // Check results and add the returned data to the main array
        if(sizeOf($result["results"])>0){
            $CDRData = array_merge($CDRData,$result["results"]);
        }
    }
    return $CDRData;
}

$CDRData= getCDRsByDate("[[DATE1]]","[[DATE2]]");
print "CDRs as Array:"; var_export($CDRData);

πŸ“˜

About the datetime fields

All the datetime fields have the GMT timezone, if you want to convert this data to your timezone you can use the function changeTimeZone explained in this article