以邮政格式将多维数组发送到Web服务

时间:2016-08-31 05:24:00

标签: php web-services

我想向Web服务发送请求,我想发送到该Web服务的数据是一个多维数组。

这是数组:

$array = array( array(
                        'search_product' => 'syntha-6 isolate One',
                        'search_product1' => 'syntha-6 isolate Two',
                        'search_product2' => 'syntha-6 isolate Three',
                        'search_product3' => 'syntha-6 isolate Four',
                        'search_product4' => 'syntha-6 isolate Five',
                        'search_brand1' => 'bsn',
                        'search_brand2' => 'BSN'
                      ),
                array(
                        'search_product' => 'syntha-6 isolate Six',
                        'search_product1' => 'syntha-6 isolate Seven',
                        'search_product2' => 'syntha-6 isolate Eight',
                        'search_product3' => 'syntha-6 isolate Nine',
                        'search_product4' => 'syntha-6 isolate Ten',
                        'search_brand1' => 'bsn',
                        'search_brand2' => 'BSN'
                      ),
                array(
                        'search_product' => 'syntha-6 isolate H',
                        'search_product1' => 'syntha-6 isolate K',
                        'search_product2' => 'syntha-6 isolate L',
                        'search_product3' => 'syntha-6 isolate M',
                        'search_product4' => 'syntha-6 isolate N',
                        'search_brand1' => 'bsn',
                        'search_brand2' => 'BSN'
                      ),
            );      

这是我用来将数据发送到网络服务的卷曲代码。

$send_data = array('data'=>$array);
$content   = json_encode($send_data);
$url       = "http://52.53.227.143/API_test1.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

$response = json_decode($json_response, true);
print_r($response);

这是我在webservices服务器上使用的代码:

$post       = json_decode($_POST);
$json_array = json_encode($post);

echo $json_array;

但它不适合我。

请建议我应该如何完成此功能。

1 个答案:

答案 0 :(得分:0)

将数组作为json和json http header

发送
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));