3

I am sending the following json by cURL to another server.

$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>";
}

And getting an error saying:

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.
j0k
  • 22,600
  • 28
  • 79
  • 90
ROBIN
  • 502
  • 6
  • 17
  • This is a server side issue. The URL you are sending the data to does not accept the data you are attempting to send. You need to re-read the API documentation for the service you are consuming, and contact their support if you believe you have received this message in error. – DaveRandom Jan 15 '13 at 10:53
  • Thanks for the reply ...I am getting this error in result .. i have access of both servers and i have been tried to exceed post_max_size etc but result is same .. :) any idea whats wrong going on – Nipun Tyagi Jan 15 '13 at 10:57
  • @user1640432 Does the `test.php` script get called or is it being blocked by the webserver before your script is run? – DaveRandom Jan 15 '13 at 11:06
  • @DaveRandom: test.php is a simple PHP file that placed on the root of the domain and in this file i trying to get the data that i have posted .. just writing $data = $_REQUEST['data']; – Nipun Tyagi Jan 15 '13 at 11:17
  • @user1640432 I've just noticed that `curl_setopt($ch, CURLOPT_POSTFIELDS, $data);` is wrong, `$data` should be `$bodyStr` - does this make a difference? As it is the `Content-Length:` header does not match the length of the data you are sending, this may be the cause of the error. If that does not fix it, please show you php.ini in a pastebin, feel free to censor any paths, passwords etc - any information specific to you system. – DaveRandom Jan 15 '13 at 11:24
  • N.B. the php.ini I would like you to show is the one from the remote server, not the one where you are running this code (unless they are the same box) – DaveRandom Jan 15 '13 at 11:29
  • @DaveRandom: yeah its fine now after this change .. But i cant access the data in the test.php file – Nipun Tyagi Jan 15 '13 at 11:29
  • @user1640432 Please show the code from `test.php`. The data should be available in `$_POST['json']` so please also show `var_dump($_POST);` – DaveRandom Jan 15 '13 at 11:31
  • @DaveRandom: the code of test.php is : if(isset($_POST)){ $data = $_REQUEST['json']; var_dump($_POST); print_r($data); } – Nipun Tyagi Jan 15 '13 at 11:36
  • @user1640432 What does that code output? Also note that you should avoid using `$_REQUEST` if possible, it's not enabled everywhere and it has a degree of unpredictability if it is. – DaveRandom Jan 15 '13 at 11:39
  • @DaveRandom: Output of the test.php is array(0) { } but am getting the same data in the curl result same as i had send – Nipun Tyagi Jan 15 '13 at 11:43
  • Try [this](http://pastebin.com/vxurAdnj) on the client side (replacing the above) and [this](http://pastebin.com/T61mwd1U) on the server. – DaveRandom Jan 15 '13 at 12:28
  • Also try `echo $bodyStr;` if that does not work, to ensure the data string you send to the server is being correctly built. – DaveRandom Jan 15 '13 at 12:30

1 Answers1

1

now i have json data

$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);
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • 1
    The content type of the data you are creating in the above code is not `application/json`. – DaveRandom Jan 15 '13 at 11:35
  • 1
    No, because you passed an array to `CURLOPT_POSTFIELDS`. The content type when you do this is `multipart/form-data`. – DaveRandom Jan 15 '13 at 11:41
  • datapost array is look like : Array ( [rcpt_to] => XXX.xxx@gmail.com [to] => XXX.xxx@gmail.com [subject] => 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] => 'here is HTML code' ); – Nipun Tyagi Jan 15 '13 at 11:57