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']);
}