PHP загрузить файл с curl

Я пытаюсь загрузить файл в API с помощью PHP. В дополнение к файлу мне нужно предоставить другие параметры. Мой текущий код выглядит так:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);

curl_setopt($ch, CURLOPT_PUT, 1);
$post = array(
'file'        => '@' . realpath('filename'),
'other_parameter'  => ''
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array('Content-Type: multipart/form-data');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Ответ заголовка сервера:

HTTP/1.1 422 status code 422
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Status: 422 Unprocessable Entity
Vary: Origin
X-Content-Type-Options: nosniff
X-Xss-Protection: 1; mode=block
Content-Length: 106
Connection: keep-alive

Также я получаю сообщение, что поле файла не может быть пустым.

0

Решение

Вот ответ на ваш вопрос (так как это повторный вопрос!):

как загрузить файл с помощью curl с php

А также взгляните на ссылку здесь:
http://php.net/manual/en/function.curl-file-create.php

1

Другие решения

function ftp_upload($localfile,$ftpHost,$ftpUsername,$ftpPassword){
$ch=curl_init();
$fp=fopen($localfile,'r');
curl_setopt($ch,CURLOPT_URL,"ftp://".$ftpUsername.':'.$ftpPassword.'@'.$ftpHost.'/htdocs/'.$localfile);
curl_setopt($ch,CURLOPT_UPLOAD,1);
curl_setopt($ch,CURLOPT_INFILE,$fp);
curl_setopt($ch,CURLOPT_INFILESIZE,filesize($localfile));
curl_exec ($ch);
$error_no=curl_errno($ch);
curl_close ($ch);
if($error_no==0){
$message='File uploaded successfully.';
}
else{
$message='File upload error: $error_no';
}
return $message;
}
echo ftp_upload('test.zip','ftp.hello.net','user','123456');
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector