2

/Unable to Send Multidimensional Json array through php cURL . so in the below code i send it as array of Objects which will be difficult to retrieve in 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 Answers 2

1

Try looking at the following example: POSTing JSON Data With PHP cURL

Useful excerpt:

$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);
Sign up to request clarification or add additional context in comments.

5 Comments

@ammu Why? Which statement does not accept the multidimensional array? json_encode() is the only statement taking the array...
What is the error you receive from the code supplied?
if i use this code and $data_string is multidimensional array then response will be NULL
@ammu Nope... $data_string will be a string with a JSON-representation of a multidimensional array. I don't know who you are sending the request to, but ask them what they would like to receive and post this in the question... Without an error, I cannot help you any further.
The mistake was in checking the response : In Response page: instead of this code...... echo $JSONData = file_get_contents("php://input"); exit(); firstly i used :.............echo json_decode($_POST);exit();
0
$data_string = json_encode($request);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                               

$request should be associated array format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.