Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

francisMS's avatar

want to assign multiple request data to cURL CURLOPT_POSTFIELDS in laravel

   $data = array("NAME" => "somename","AGE" => "22");
   $fileData = sampleImg.jpg;  //Image file.

I want to assign $data and $fileData into the cURL CURLOPT_POSTFIELDS, but the post_fields allowed assign only one data.

Anyone can help!...

this is my cURL code...

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data, $fileData);                                 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    curl_close($ch);
0 likes
2 replies
RamjithAp's avatar

Merge your data as single array and then post.

$data = array("NAME" => "somename","AGE" => "22");
$fileData = sampleImg.jpg;  //Image file.
$alldata = array("data" => $data,"fileData " => $fileData);

 $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $alldata);                                 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    curl_close($ch);
francisMS's avatar
francisMS
OP
Best Answer
Level 1

Thanks for your support @RamjithAp

Yes, your process is correct but change the 'Content-Type'.

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data));

This is the code...

     $postfields = array('data'=>$data,'fileData'=>$fileData);


     $url = 'http://postURL';
     $ch = curl_init();

     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $result = curl_exec($ch);

     curl_close($ch);

Please or to participate in this conversation.