-1

I want to send these three array data into the external api. For that firstly I encode this into JSON, but always it sends the last array.

$params = array(        array('receiverName' => 'sample_name',
    'receiverEmail' => 'krishanuniyalas@gmail.com',
    'senderEmail' => 'info@hotelpalacio.net',
    'roomNumber' => '12456',
    'accessToken' =>'ca5629d0-6810-11e8-9d40-d7194ac0ac8d'),array('receiverName' => 'sample_name',
    'receiverEmail' => 'karamuniyalas@gmail.com',
    'senderEmail' => 'info@hotelpalacio.net',
    'roomNumber' => '12456',
    'accessToken' =>'ca5629d0-6810-11e8-9d40-d7194ac0ac8d'),array('receiverName' => 'sample_name',
    'receiverEmail' => 'krishanuniyalas@gmail.com',
    'senderEmail' => 'info@hotelpalacio.net',
    'roomNumber' => '12456',
    'accessToken' =>'ca5629d0-6810-11e8-9d40-d7194ac0ac8d'));

$data_string = json_encode($params);

foreach($params as $pa) {
  $r=json_encode($pa);
}

curl_setopt($ch, CURLOPT_POSTFIELDS, $r);
glennsl
  • 28,186
  • 12
  • 57
  • 75
  • What's the output when you encode it? What does the server get when you make this request? Are you sure the server can handle this request like you want it to? – Chad K Aug 08 '18 at 20:06
  • you can only post data once. Why don't you send `$data_string`? – Liora Haydont Aug 08 '18 at 20:07
  • Where 's the rest of your code? Is `curl_setopt($ch, CURLOPT_POSTFIELDS, $r)` all? What about setting the option for getting a response? What about sending the data and waiting for a response? – Marcel Aug 08 '18 at 20:09
  • foreach($jsondata as $data) { $array[]=$data; } $z=$array; $yy=array(); foreach($z as $t) { $data5 = json_encode($t,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$data5); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type:application/json', 'cache-control: no-cache', 'accessToken:casdfgsd810-11esdfg8-9d40-d7194ac0ac8d')); }$result = curl_exec($ch); the problem was $result = curl_exec($ch);.i was using it outside the foreach loop...otherwise everything was work – krishan kant Sharma Aug 09 '18 at 22:25

2 Answers2

0

Here 's a working example ...

$data = [
    [
        'a' => 'bla',
    ],
    [
        'b' => 'blubb',
    ],
];

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, "http://www.example.com/");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

$result = [];
foreach ($data as $value) {
    $json = json_encode($value);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [                                                                         
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($json),                                                                       
    ]);
    $result[] = curl_exec($handle);
}

Open a valid curl resource and set the corresponding curl options for your purpose. You want to send a json string as post request. Furthermore you should tell the other side, that you send json data. Say curl, that you 're awaiting a response and the fire every time running through the foreach loop. That 's all ...

If you want to send the whole data at once leave out the foreach loop end encode the whole data array and send it.

Marcel
  • 4,854
  • 1
  • 14
  • 24
  • the only problem was with $result[] = curl_exec($handle);..i was using it outside the foreach loop..otherwise everything was work perfectly..but thank you once again – krishan kant Sharma Aug 09 '18 at 22:29
-1

You need to just create a simple query params like $data = http_build_query($params) And then pass data to your curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

Ajay Kumar
  • 99
  • 6