Welcome Guest, Not a member yet? Register   Sign In
CURL file upload
#1

Hi,
I am trying to upload images via and API using CI CURL Request class. However, Codeigniter errors "CodeIgniter\HTTP\Exceptions\HTTPException 22 : The requested URL returned error: 400" referring to line 'file=>$myfile'

PHP Code:
$image = \Config\Services::image();

$data['job_id'] = $this->request->getPost('job_id');
$imagefiles $this->request->getFiles();

$image_write_path $_SERVER['DOCUMENT_ROOT'].'/images/projects/task_no_'.$data['job_id'];

if (!
file_exists($image_write_path)) {
    mkdir($image_write_path0777true);
}

$client service('curlrequest', [
    'baseURI' => 'BASE OF API PATH',
]);


foreach(
$imagefiles['images'] as $img) {
    list($width$height) = getimagesize($img);
    $size=filesize($img);

    //save temporary file and move image (with original image name) to server filesystem
        if(! $img->hasMoved()) {
    $originalName $img->getClientName();
    $filename pathinfo($originalNamePATHINFO_FILENAME);

    $filepath WRITEPATH 'uploads/' $img->store();
    $new_file_put$image_write_path.'/'.$filename.'.png';
    $image_view_path base_url().'/images/projects/task_no_'.$data['job_id'].'/'.$filename.'.png';

    if( ! \Config\Services::image('imagick')
        ->withFile($filepath)
        ->convert(IMAGETYPE_PNG)
        ->save($new_file_put)) {
        unlink($filepath);
    }

    $myfile = new \CURLFile($image_view_path);

    $upload_image $client->post('API ENDPOINT',
        [
            'headers' => [
            'Content-Type: multipart',
            'Authorization' => ['***MYTOKEN***'],
            ],
            [
            'file' => $myfile
            
]
        ]
    );

    $response json_decode($body);
    log_message('notice''[SUCCESS] {file}-{line}, RESPONSE - '.print_r($responsetrue));
}; 

Above throw the HTTP errors!!

However, using native curl with  CI \CURLfile (at bottom) I do not get the error.
PHP Code:
$myfile = new \CURLFile($image_view_path); 

PHP Code:
foreach($imagefiles['images'] as $img) {
    list($width$height) = getimagesize($img);
    $size=filesize($img);

    //save temporary file and move image (with original image name) to server filesystem
        if(! $img->hasMoved()) {
    $originalName $img->getClientName();
    $filename pathinfo($originalNamePATHINFO_FILENAME);

    $filepath WRITEPATH 'uploads/' $img->store();
    $new_file_put$image_write_path.'/'.$filename.'.png';
    $image_view_path base_url().'/images/projects/task_no_'.$data['job_id'].'/'.$filename.'.png';

    if( ! \Config\Services::image('imagick')
        ->withFile($filepath)
        ->convert(IMAGETYPE_PNG)
        ->save($new_file_put)) {
        unlink($filepath);
    }

    $myfile = new \CURLFile($image_view_path);
    $curl curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => '/FULL/API/PATH',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array('file'=> $myfile),
        CURLOPT_HTTPHEADER => array(
        'Content-Type: multipart/form-data',
        'Authorization: ***MY TOKEN***'
    ),
));

$response curl_exec($curl);

curl_close($curl); 
 
log_message('notice''[SUCCESS] {file}-{line}, RESPONSE - '.print_r($responsetrue)); 

Above is successful.
Any help would be appreciated.
Reply
#2

Try
PHP Code:
$client->post('API ENDPOINT', [
    // ...
    'multipart' => [
        'file' => $myfile,
    ],
]); 

See https://www.codeigniter.com/user_guide/l...#multipart
Reply
#3

(07-30-2024, 07:55 PM)kenjis Wrote: Try
PHP Code:
$client->post('API ENDPOINT', [
    // ...
    'multipart' => [
        'file' => $myfile,
    ],
]); 

See https://www.codeigniter.com/user_guide/l...#multipart

Thank you so much. Works like a dream
Reply




Theme © iAndrew 2016 - Forum software by © MyBB