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);
}

Decode data from JSON encode Code Igniter

function doSendNotifBirthDay(){
    $data = array();
    $dataz = array();

    $this->db->select('warga_id,warga_nama,warga_tgl_lahir,warga_telepon,warga_photo');
    $this->db->where('DATE_FORMAT(warga_tgl_lahir,"%d-%m")','DATE_FORMAT(CURDATE(),"%d-%m")',FALSE);
    $this->db->where('warga_jenis',1);
    $this->db->where('warga_status',1);

    $q = $this->db->get('data_warga');

    if($q->num_rows > 0){
        $data['result']=true;
        $data['data']=$q->result();
    }else{
        $data['result']=false;
        $data['msg']='data tidak ditemukan';
    }

    $result = json_encode($data);
    $json_decoded = json_decode($result,true);

    if($q->num_rows > 0){

        $dataz['result']=true;
        $dataz['msg']='data ditemukan';

        foreach ($json_decoded['data'] as $row){
            $nama  = $row['warga_nama'];
            $phone = $row['warga_telepon'];

            $isi ='Barakallahu fii umrik *'.$nama.'*, bertambah satu tahun lagi usiamu hari ini *'.$this->thisDay().'*, tanggal *'.date('d').' '.$this->thisMonth().' '.date('Y').'* dan hanya doa yang bisa kami haturkan. Semoga Allah memberikan kesehatan, rezeki yang melimpah dan kesuksesan, serta umur panjang. Dan tak lupa, semoga selalu diberi keberkahan Allahuma Aamiin...🤲'."\n\n".';

            $simpan['phone']=$phone;
            $simpan['pesan']=$isi;
            $simpan['id_users']='139';
            $this->db->insert('wa_outbox',$simpan);
        }
    }else{
        $dataz['result']=false;
        $dataz['msg']='data tidak ditemukan';
    }
    echo json_encode($dataz);
}

function thisDay(){
    $day = date ("D");

    switch($day){
        case 'Sun':
            $hari_ini = "Minggu";
        break;

        case 'Mon':         
            $hari_ini = "Senin";
        break;

        case 'Tue':
            $hari_ini = "Selasa";
        break;

        case 'Wed':
            $hari_ini = "Rabu";
        break;

        case 'Thu':
            $hari_ini = "Kamis";
        break;

        case 'Fri':
            $hari_ini = "Jumat";
        break;

        case 'Sat':
            $hari_ini = "Sabtu";
        break;
        
        default:
            $hari_ini = "Tidak di ketahui";     
        break;
    }
    return $hari_ini;
}

function thisMonth(){
    $month = date("m");

    switch($month){
        case '01':
            $month = "Januari";
        break;

        case '02':         
            $month = "Februari";
        break;

        case '03':
            $month = "Maret";
        break;

        case '04':
            $month = "April";
        break;

        case '05':
            $month = "Mei";
        break;

        case '06':
            $month = "Juni";
        break;

        case '07':
            $month = "Juli";
        break;
        case '08':
            $month = "Agustus";
        break;
        case '09':
            $month = "September";
        break;
        case '10':
            $month = "Oktober";
        break;
        case '11':
            $month = "November";
        break;
        case '12':
            $month = "Desember";
        break;
        default:
            $month = "Unknow";     
        break;
    }
    return $month;
}  

Convert JSON file into CSV in PHP

<?php
   
// Student JSON data
$jsondata
  '[
   {"student":"sravan kumar","age":22,"subject":"java"},
   {"student":"ojaswi","age":21,"subject":"java"},
   {"student":"rohith","age":22,"subject":"dbms"},
   {"student":"bobby","age":22,"subject":"sql"}]';
   
// Decode json data and convert it
// into an associative array
$jsonans = json_decode($jsondata, true);
   
// CSV file name => geeks.csv
$csv = 'geeks.csv';
   
// File pointer in writable mode
$file_pointer = fopen($csv, 'w');
   
// Traverse through the associative
// array using for each loop
foreach($jsonans as $i){
      
    // Write the data to the CSV file
    fputcsv($file_pointer, $i);
}
   
// Close the file pointer.
fclose($file_pointer);
  
?>
 
sumber : https://www.geeksforgeeks.org/how-to-convert-json-file-into-csv-in-php/

Manage user MySQL 5.1 Command line

#create user
CREATE USER 'user'@'localhost' IDENTIFIED BY 'Passwordxxx**';
GRANT SELECT, LOCK TABLES, CREATE, DROP, INSERT, UPDATE, EVENT ON * . * TO 'user'@'localhost';
FLUSH PRIVILEGES;

#drop user
DROP user 'user'@'localhost';

#change password user MySql
SET PASSWORD FOR 'user'@'localhost' = PASSWORD('user@localhost234#');    #mysql under 8
ALTER USER 'user'@'localhost' IDENTIFIED BY 'user'@localhost234#';    #mysql 8

#change definer
UPDATE `mysql`.`proc` p SET definer = 'user@%' WHERE definer='user@%' AND db='dbname';

#show grant for user
SHOW GRANTS FOR 'user'@'localhost;

#dump procedure only
mysqldump --single-transaction -u username -p db > db.sql

#find definer
select v.TABLE_SCHEMA, v.TABLE_NAME, v.`DEFINER`
from information_schema.VIEWS v
where v.TABLE_SCHEMA not in ('sys');

#revoke user
REVOKE DELETE ON `dbname`.* FROM 'user'@'localhost';
FLUSH PRIVILEGES;

#grant procedure or function only
GRANT EXECUTE ON PROCEDURE `dbname`.`procName` TO 'user'@'localhost';
GRANT EXECUTE ON FUNCTION `dbname`.`funcName` TO 'user'@'localhost';
FLUSH PRIVILEGES;