我通过cURL将以下json发送到另一台服务器。
$postUrl = 'http://anotherserver.com/test.php';
$this->datatopost = array (
"rcpt_to" => $rcpt_to,
"to" => $to,
"subject" => $subject,
"header" => $headers,
"body" => $body,
);
$data = array (
'json' => json_encode($this->datatopost)
);
$bodyStr = http_build_query($data);
$postlength = strlen($data);
$ch = curl_init('www.xxx.com/test.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($bodyStr))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo"<pre>"; print_r($info);
echo"</pre>";
}
收到错误说:
Request Entity Too Large
The requested resource
/test.php
does not allow request data with POST requests, or the amount of data provided in
the request exceeds the capacity limit.
答案 0 :(得分:1)
现在我有json数据
$postUrl = 'http://your domain/';
$datatopost = array (
"rcpt_to" => 'ffgf',
"to" => 'gdfhfh',
"subject" => 'dgfdfh',
"header" => 'sfgdh',
"body" => 'DFSGf',
);
$datatopost = json_encode($datatopost);
print_r($datatopost) //{"rcpt_to":"ffgf","to":"gdfhfh","subject":"dgfdfh","header":"sfgdh","body":"DFSGf"}
$data = array ('json' => json_encode($datatopost));
$ch = curl_init($postUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);