通过PHP Curl上传CSV文件

时间:2017-05-25 06:53:52

标签: php csv curl file-upload

我确定已经多次询问过这个问题,但我无法弄清楚为什么我的php脚本无效。我有一个CSV文件,我知道当我使用以下命令行curl上传它时它完美无缺:

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 Curl时,这是我的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导入标题。这没有任何意义。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files