我相信这个问题已经问过很多次了,但是我不知道为什么我的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脚本,当我试图将这个命令转换为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导入头。这没有任何意义。有什么想法吗?
发布于 2017-05-25 07:08:06
而不是试图访问您的下载文件夹,上传项目目录中的文件,然后将其传递给curl。
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);
https://stackoverflow.com/questions/44174158
复制相似问题