Как отправить файл .csv в API с помощью curl - PullRequest
0 голосов
/ 03 ноября 2019

Я загружаю файл для загрузки в папку загрузки, к сожалению, я не могу использовать curl для отправки его в API, созданный другим человеком

if(isset($_POST['upload'])){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{

//Initialise the cURL var
$ch = curl_init();

//Create a POST array with the file in it
$postData = array(
    'file' => $uploadfile
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);


// Need to set this head for uploading file.
$headers = array("Content-Type" => "multipart/form-data");

//Set the Url
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://localhost/CASE_2/api/importClients?token={$token}");
 //Get the response from cURL
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  


// Execute the request
$response = curl_exec($ch);
print_r($response);
 // echo "The file has been uploaded successfully";
 // return $uploadfile;
}
else
{
  echo "There was an error uploading the file";
  return null;
}}

Я исправил его и преобразовал код. К сожалению, он все еще не сохраняет мои данные, если (isset ($ _ POST ['upload'])) {

$uploaddir = "uploads/";
$uploadfile = $uploaddir . basename($_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{

    if (function_exists('curl_file_create')) {
        $cFile = curl_file_create($uploadfile);
      } else { //
        $cFile = '@' . realpath($uploadfile);
      }
      $post = array('file'=> $cFile);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,"http://localhost/CASE_2/api/importClients?token={$token}");
      curl_setopt($ch, CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
      $result=curl_exec ($ch);
      curl_close ($ch);   
}

} Я хотел, чтобы файл был отправлен в уже созданный API, к сожалению, намомент, когда я получил сообщение об ошибке:

Notice: Undefined index: file in D: \ xampp \ htdocs \ CASE_2 \ Classes \ Helper.php on line 246

Notice: Undefined index: file in D: \ xampp \ htdocs \ CASE_2 \ Classes \ Helper.php on line 248

There was an error uploading the file
Fatal error: Uncaught TypeError: Argument 1 passed to League \ Csv \ AbstractCsv :: createFromPath () must be of the type string, null given.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...