问题
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 ...
回答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);
回答2:
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);
回答3:
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']);
}
来源:https://stackoverflow.com/questions/35336049/how-to-post-json-data-of-multidimentional-array-using-curl