2017-05-25 70 views
2

我確定這個問題已經被問了很多次,但我不明白爲什麼我的PHP腳本不工作。我有一個CSV文件,我知道它完美的作品,當我把它上傳使用下面的命令行卷曲:通過PHP上傳一個CSV文件捲曲

curl -H 'Content-Type: text/csv' --data-binary @/Users/johndoe/Downloads/payables.csv -H "Authorization: Bearer [some_token_key]" 'https://example.com/api/v1/imports.json' 

這是我的PHP腳本,當我試圖把這種命令到PHP捲曲:

$file = "/Users/johndoe/Downloads/payables.csv"; 
$authorization = "Authorization: Bearer [some_token_key]"; 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, "https://example.org/api/v1/imports.json"); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_HTTPHEADER, [$authorization, 'Content-Type: text/csv']); 
$cfile = new CurlFile($file, 'text/csv'); 
$data = array('data-binary' => realpath($cfile)); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$curl_response = curl_exec($curl); 
curl_close($curl); 

有沒有人看到我的PHP腳本有什麼問題?我正在使用php 5.5,並且在接收網站上看到的嘗試處理CSV文件的錯誤是,它找不到CSV導入標題。這沒有任何意義。有任何想法嗎?

+0

我想你的PHP腳本無法從/Users/johndoe/Downloads/payables.csv讀取文件,把它在你的PHP腳本所在的根目錄,即並嘗試 – sumit

+0

@sumit沒有工作。我把文件放在我的應用程序的根目錄下,什麼也沒有。同樣的錯誤。 – user1370897

回答

1

而不是嘗試訪問您的下載文件夾上傳項目目錄中的文件,然後將其傳遞到捲曲。

move_uploaded_file($_FILES['file']['tmp_name'], __DIR__.'/uploads/'. $_FILES["image"]['name']); 

$file = "uploads/payables.csv"; 
$authorization = "Authorization: Bearer [some_token_key]"; 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, "https://example.org/api/v1/imports.json"); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_HTTPHEADER, [$authorization, 'Content-Type: text/csv']); 
$cfile = new CurlFile($file, 'text/csv'); 
//curl file itself return the realpath with prefix of @ 
$data = array('data-binary' => $cfile); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$curl_response = curl_exec($curl); 
curl_close($curl);