2016-08-31 152 views
1

我想發送一個請求到一個web服務,我想發送給那個webservices的數據是一個多維數組。發送多維數組以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' 
        ), 
      );  

而這裏的,我使用的數據發送到web服務的捲曲代碼。

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

這是我使用的web服務代碼斷絕:

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

echo $json_array; 

但它不是爲我工作。

請建議我該如何完成此功能。

回答

0

發送數組作爲JSON和JSON的HTTP頭

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

感謝您的回答,但我已經厭倦了這種解決方案。它不適合我。 –

+0

你檢查了這些東西的結果嗎?$ output = curl_exec($ ch); $ err = curl_errno($ ch); $ errmsg = curl_error($ ch); $ header = curl_getinfo($ ch);' – Tamil

+0

我跟着錯誤: 警告:json_decode()預計參數1爲字符串,陣列上線/var/www/html/API_test1.php給出23 空 這裏是我的webserivce代碼: $內容= json_decode( $ _ POST,真正的); $ json_array = json_encode($ content); echo $ json_array; –