2014-12-03 62 views
2

/無法通過php cURL發送多維Json數組。 所以在下面的代碼我發送它作爲將難以在python檢索對象的陣列/如何通過php cURL發送多維Json數組

$_api_url="http://example.com" ; 
    $params = http_build_query(array('data_Details' => json_encode($request))); 
    //initialize and setup the curl handler   
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $_api_url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$params); 

    //execute request 
    $result = curl_exec($ch); 

    //close connection 
    curl_close($ch); 

回答

1

嘗試尋找在下面的例子:POSTing JSON Data With PHP cURL

有用摘錄:

$data = array("name" => "Hagrid", "age" => "36");                  
$data_string = json_encode($data);                     

$ch = curl_init('http://api.local/rest/users');                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);                             

$result = curl_exec($ch); 
+0

@ammu爲什麼?哪個語句不接受多維數組? 'json_encode()'是接受數組的唯一語句... – RichardBernards 2014-12-03 12:19:58

+0

您從提供的代碼中收到的錯誤是什麼? – RichardBernards 2014-12-03 12:25:50

+0

如果我使用此代碼,$ data_string是多維數組則響應將是NULL – ammu 2014-12-03 12:37:35

0
$data_string = json_encode($request); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);        

$ request應該是關聯的數組格式。