通过PHP上传一个CSV文件卷曲

问题描述:

我确定这个问题已经被问了很多次,但我不明白为什么我的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

而不是尝试访问您的下载文件夹上传项目目录中的文件,然后将其传递到卷曲。

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