3

I am trying to develop small API code in php, which will send POST request in json format using PHP curl (from client side). And at receiving side(at server side) I want to parse the request and process it. but at receiving side it gives me empty array in post field. Below is my code

Client side:

<?php

    $data = array("name" => "Hagrid", "age" => "36");                                                                    
    $data_string = json_encode($data);                                                                                   
    $ch = curl_init($url);                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, "xdata=".$data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   
    echo $result = curl_exec($ch);

?>

At server side:

<?php

    print_r($_POST);

?>

Blockquote Array ( ) Blockquote

It always gives me empty POST array at server side. Am I doing things wrong or is there another way to parse the request? Please guide me.

Shirish
  • 51
  • 1
  • 3

2 Answers2

6

You can use php://input read-only stream to access JSON post data instead of $_POST like this

<?php 
    $json = file_get_contents('php://input');
?>

It will give you POST data as is. You will be able to decode it using json_decode() later.

Example code -

<?php 
   $json = json_decode(file_get_contents('php://input'));
?>
Vikash Kumar
  • 981
  • 8
  • 15
  • Thanks Vikesh, It is working now. but it is not possible to get the data from POST? – Shirish Feb 18 '16 at 06:30
  • @Shirish, `$_POST` is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. For the reference see this link http://php.net/manual/en/reserved.variables.post.php – Vikash Kumar Feb 18 '16 at 06:34
  • If my answer is helpful then accept this. – Vikash Kumar Feb 18 '16 at 06:36
  • " application/x-www-form-urlencoded" is worked in my case. Thanks for help. – Shirish Feb 18 '16 at 07:17
0

You are setting wrong content length with the CURLOPT_HTTPHEADER header. Your overall post string is strlen("xdata=".$data_string) whereas you are using strlen($data_string).

Also, you are using application/x-www-form-urlencoded for posting, but mentioning the content type forcefully as application/json

So remove the following block from your code and you should be fine.

// Remove it
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
Sabuj Hassan
  • 35,286
  • 11
  • 68
  • 78