我想将这三个数组数据发送到外部api。为此,我首先将其编码为JSON,但始终会发送最后一个数组。
$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);
答案 0 :(得分:0)
这是一个可行的例子...
$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);
}
打开有效的curl资源,并根据需要设置相应的curl选项。您要发送json字符串作为发布请求。此外,您应该告诉另一端,您发送json数据。说卷曲,您每次在foreach循环中运行时都在等待响应和大火。就这些...
如果您想一次发送全部数据,请忽略foreach循环末尾,对整个数据数组进行编码并发送。
答案 1 :(得分:-1)
您只需要创建一个简单的查询参数,例如$data = http_build_query($params)
然后将数据传递到您的curl_setopt($ch,CURLOPT_POSTFIELDS,$data)
;