如何通过php cURL发送多维Json数组

时间:2014-12-03 12:03:14

标签: php json curl multidimensional-array

/ 无法通过php cURL发送多维Json数组。           所以在下面的代码中我将它作为对象数组发送,这将很难在python中检索 /

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

$ request应该是关联的数组格式。