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);
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);
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.