使用cURL将Json数据发送到另一台服务器

我通过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.

现在我有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);
链接地址: http://www.djcxy.com/p/8557.html

上一篇: Sending Json Data to another server USING cURL

下一篇: PHP POST curl terminal command in PHP script