Request Entity Too Large – php curl error

I have been trying to make curl post request to a script like so:

    $ch = curl_init('http://my.server/api.php');                     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => 'hello' ));
    curl_setopt($ch, CURLOPT_POST, true);                                         
    $response = curl_exec($ch);   

It keep failing with an Apache 213 error like this:

The requested resource
/api.php
does not allow request data with POST requests, or the amount of data provided in the request exceeds the capacity limit.

It turns out that you need to set the curl options in the correct order and CURLOPT_POST needs to be before CURLOPT_POSTFIELDS. Like so:

    $ch = curl_init('http://my.server/api.php');                     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
    curl_setopt($ch, CURLOPT_POST, 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => 'hello' ));
true);                                         
    $response = curl_exec($ch);   

Leave a Reply