Я пытаюсь сделать POST-запрос к своему бэкэнду, и мне нужно прикрепить
token
и одно или несколько изображений files
.
Моя версия PHP 5.6, и я использую CurlFile для создания файла.
Запрос никогда не поступает на сервер.
Код PHP
$file_name_with_full_path = $_FILES["file-0"]["tmp_name"]; //tested all good
$target_url = $app_engine_url."images/upload";
$accessToken = $_SESSION['token'];
$imageName = basename($_FILES["file-0"]["name"]);
$filesize = $_FILES['file-0']['size'];
$postfields = array("file" => makeCurlFile($file_name_with_full_path), "filename" => $imageName,"access_token"=>$accessToken);
$headers = array("Content-Type:multipart/form-data");
$ch = curl_init();
$options = array(
CURLOPT_URL => $target_url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
$resp = curl_exec($ch);
echo $resp;
if(!curl_errno($ch)) {
$info = curl_getinfo($ch);
if ($info['http_code'] == 200)
$errmsg = "File uploaded successfully";
}
else
{
$errmsg = curl_error($ch);
}
print_r(curl_getinfo($ch));
curl_close($ch);
echo stripslashes($errmsg);
return null;
}
function makeCurlFile($file){
$info = getimagesize($file);
$mime = $info['mime']; etc.
$name = basename($_FILES["file-0"]["name"]);
$output = new \CURLFile($file, $mime, $name);
return $output; //Object is also created successfully (tested by calling
//methods)
}
Ответ PHP
Array
(
[url] => someurl.com //URL is correct (double checked)
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.031
[namelookup_time] => 0.015
[connect_time] => 0.031
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 172.217.212.153
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 10.128.0.3
[local_port] => 49858
)
Кто-нибудь может указать, что я здесь делаю неправильно?
Спасибо.