2016-08-31 7 views
1

Webサービスにリクエストを送信したいと思います。その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' 
        ), 
      );  

そして、ここでは、私が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(:json_decode()は、パラメータ1が文字列であることを期待し、ライン上の/var/www/html/API_test1.phpに指定された配列23 ヌル ここでは私のwebserivceコードです$ _POST、true); $ json_array = json_encode($ content); echo $ json_array; –