Kamis, 17 Agustus 2023

CURL with post field Code Igniter

 public function getDataToCsv(){
    $data = array();

    $kode = $this->input->post('kode');

    $url = 'http://[domain]/app/api/getData';
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'f_kode='.$kode);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    curl_close($ch);

    if($result){

        $json_decoded= json_decode($result,true);
        $fileName = date('YmdHis').'.csv';
        $csv = '/var/www/html/app/file/csv/'.$fileName;
        $file_pointer = fopen($csv, 'w');
        foreach($json_decoded['data'] as $i){
            fputcsv($file_pointer, $i);
        }
        fclose($file_pointer);
        $data['result'] = true;
        $data['file_name']=$fileName;
        $data['msg']='Export to '.$fileName.' success';
    }else{
        $data['result']=false;
        $data['msg']='Tidak ada data';
    }
    echo json_encode($data);
}

Tidak ada komentar:

Posting Komentar