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

3

我正在使用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.

@DaveRandom:test.php是一个简单的PHP文件,放置在域名的根目录下,在这个文件中,我试图获取我已经发布的数据...只需编写$data = $_REQUEST['data']; - Nipun Tyagi
请注意:我想要您展示的php.ini是远程服务器上的,而不是您运行此代码的那个(除非它们是同一台服务器)。 - DaveRandom
@user1640432,请展示test.php中的代码。数据应该在$_POST['json']中可用,所以请同时展示var_dump($_POST); - DaveRandom
在客户端尝试使用这个(替换上面的内容),并在服务器上使用这个 - DaveRandom
如果 echo $bodyStr; 无法正常工作,请尝试使用该语句,以确保您发送到服务器的数据字符串被正确构建。 - DaveRandom
显示剩余8条评论
1个回答

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);

1
你在上述代码中创建的数据的内容类型不是 application/json - DaveRandom
1
不行,因为你将一个数组传递给了 CURLOPT_POSTFIELDS。这样做时的内容类型是 multipart/form-data - DaveRandom
datapost数组看起来像这样: Array ( [rcpt_to] => XXX.xxx@gmail.com [to] => XXX.xxx@gmail.com [subject] => 主题(HTML) [header] => Message-ID: Return-Path: RRRb@sent.com Date: Tue, 15 Jan 2013 03:56:14 -0800 From: "Lake Liberty" Reply-To: RRRb@sent.com MIME-Version: 1.0 X-Mailer-LID: 1 List-Unsubscribe: , X-Mailer-RecptId: 473952 X-Mailer-SID: 126 X-Mailer-Sent-By: 2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: 8bit [body] => '这里是HTML代码' ); - Nipun Tyagi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接