0

My code is

$urltopost = "http://example.com/webservice/service.php";
$datatopost = array (0 =>array('a'=>'b','c'=>'d'),1 =>array('a'=>'b','c'=>'d'),2 =>array('a'=>'b','c'=>'d'),3 =>array('a'=>'b','c'=>'d'));
$ch = curl_init ($urltopost);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($datatopost));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);

i am not getting this json to my url ...

2
  • In your service.php, did you have header set to accept json data? Check this stackoverflow.com/a/18867369/2324206 Commented Feb 11, 2016 at 10:04
  • can you print json_encode($datatopost) Commented Feb 11, 2016 at 10:05

3 Answers 3

1

finally got the solution here it is. and it's working with multidimentional array.

$urltopost = "http://example.com/webservice/service.php";
$datatopost = array (0 =>array('a'=>'b','c'=>'d'),1 =>array('a'=>'b','c'=>'d'),2 =>array('a'=>'b','c'=>'d'),3 =>array('a'=>'b','c'=>'d'));
$post_data = array('data' => serialize($datatopost));

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);

echo "<pre>";
print_r(unserialize($returndata));

service.php code

$temp = unserialize($_POST['data']);
echo serialize($temp);
Sign up to request clarification or add additional context in comments.

Comments

0

try something like this: (put your json string length into content-length)

$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 Comment

you are sennding simple one dimentional array. i am trying post multidimentional array
0

You are trying to post raw JSON data on service.php.
Passing raw JSON data doesn't populate $_POST array. On service.php page you need to catch passed data from php stdin stream like shown below:

$fp = fopen("php://input", "r");
$data = stream_get_contents($fp);
$decoded_json_data = json_decode($data);

var_dump($decoded_json_data);

Also there is another approach which lets you to populate $_POST array:

    $urltopost = "http://example.com/webservice/service.php";
    $datatopost = array (0 =>array('a'=>'b','c'=>'d'),1 =>array('a'=>'b','c'=>'d'),2 =>array('a'=>'b','c'=>'d'),3 =>array('a'=>'b','c'=>'d'));
    $data_string = http_build_query(['json' => json_encode($datatopost)]);

    $ch = curl_init($urltopost);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded',
  'Content-Length: '.strlen($data_string)]);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $returndata = curl_exec($ch);

Now, on service.php page you can access your multidimensional array in the following way:

if (isset($_POST['json'])){
    $data = json_decode($_POST['json']);
}

6 Comments

removed json_encode but showing me error "Array to string conversion"
@SatejInfotech, I apologise. Yes, let the json_encode remain
its ok , can we post multidimentional array with curl ?? if yes how ??
what gives you var_dump($decoded_json_data); when dealing with the above approach ? Or, maybe, you wanted to work only with $_POST array?
var_dump($decoded_json_data); gives nothing. its empty.
|

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.