Upload file using Curl and receive them in Laravel method

I have been trying to upload file to using curl using laravel app end point.

Below code is on separate server.

$header = $this->getCurlHeader();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);   
$cfile = new CURLFile($this->filePath . $this->csvFile,  'text/csv','text.csv');
//print_r($cfile);exit;
$postData = array('csv' => $cfile);         
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);                    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$response = curl_exec($ch);
$hhtpCode = curl_getinfo($ch);
curl_close($ch);
//$this->dbg($hhtpCode);
$this->dbg(json_decode($response,1),1);

And receiving end in laravel app hosted on different server.
public function insert(Request $request, $feed=''){
     //return response()->json(var_dump($request->all()));
     //return response()->json("here in insert");
     return response()->json($_FILES);
     echo "here";
     dd($_FILES);
}

It returns me empty response.
I am able to verify curl request using headers.

Any suggestion will be helpful.
Thanks.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can try something like this on the source server instead:

$target_url = 'https://mywebsite.com'; // Write your URL here
$dir = '/var/www/html/storage/test.zip'; // full directory of the file

$cFile = curl_file_create($dir);
$post = array('file'=> $cFile); // Parameter to be sent

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=json_decode(curl_exec($ch));
curl_close ($ch);

From the destination server, print the request sent.
public function insert(Request $request){
    dd($request->file);
}

Method 2

please use curlfile method to upload files.

if(isset($_FILES) && !empty($_FILES['identity_doc']))
            {

                    foreach($_FILES['identity_doc'] as $key => $file)
                    {
                        $data['document'.$i]= new CURLFile($_FILES['identity_doc']['tmp_name'][$i], $_FILES['identity_doc']['type'][$i],$_FILES['identity_doc']['name'][$i]);
                    }

            }

Method 3

if(isset($_POST['submit']))
{
    $fileName=$_FILES['resume']['name']; 
    $type=$_FILES['resume']['type'];
    $tempPathname=$_FILES['resume']['tmp_name'];
    $handle = fopen($tempPathname, "r");
    $POST_DATA = fread($handle, filesize($tempPathname));

    $url="https://objectstorage.region-name.oraclecloud.com/p/par-id/n/namespace`enter code here`/b/bucket-name/o/images/".$fileName;
    $curl = curl_init();
    curl_setopt_array($curl, array(
         CURLOPT_URL => $url,
         CURLOPT_RETURNTRANSFER => true,
         CURLOPT_ENCODING => "",
         CURLOPT_MAXREDIRS => 10,
         CURLOPT_TIMEOUT => 30,
         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
         CURLOPT_CUSTOMREQUEST => "PUT",
         CURLOPT_POSTFIELDS=>$POST_DATA ,
         CURLOPT_HTTPHEADER => array(
           'Content-Type: '.$type,
         ),
     ));
     $response = curl_exec($curl);
     $err = curl_error($curl);
    
     curl_close($curl);
     
     if ($response) {
        echo 'error';
     } else { 
         echo $url;
     }
}
?>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="resume">
    <button type="submit" name="submit">submit</button>
</form>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x