跳到主內容

PHP_使用curl 上傳資料

client shell

# curl 上傳資料
# {file} : 可更改,$_file 的key就會不同
# curl -X POST -F "{file}=@{檔案路徑}" {api url}
curl -X POST -F "file=@/tmp/sms_count.csv" http://192.168.1.1/api/v1/SMS/UploadSmsCount

server 端 php


// 取得$_file 資料
// $file 
Array
(
    [file] => Array
        (
            [name] => sms_count.csv
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpAvxPxQ
            [error] => 0
            [size] => 1175
        )

)
  
// 取得內容 => $data
$data = file_get_contents($_FILES['file']['tmp_name']);  

// 如果csv 是 big5  記得轉碼
$data = iconv("BIG5","UTF-8",$data);