简体   繁体   中英

PHP Curl Request Attach CurlFile Not Working

I'm trying to make POST request to my backend, and I need to attach a token and one or more image files .

My PHP Version is 5.6 and I'm using CurlFile to create the file. The request is never received on the server.

PHP Code

$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 Response

 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
)

Can anyone point out what I'm doing wrong here? Thanks.

The problem in my case was that CurlFile could not find $_FILES["file-0"]["tmp_name"] due to a permission issue I assume. I debugged this by manually placing a file within the www-root folder and hardcoding $file_name_with_full_path with that files path, in this case it worked. However, all the temp files are stored in the Windows folder and cURL could not find it. It could be a possible bug I'm not sure.

What I did was I changed the location of the upload_tmp_dir in the php.ini file to www-root directory where cURL could access the temp files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM