How to send large json data to api using php curl?

Viewed 333

Post Large amount of data from server A to B.

I am using Laravel apis. I need to post large amount of data (>8mb) using PHP Curl from server a to Laravel API. If data size is less than 5mb, it is working fine but when data amount is large, is shows empty post data on Laravel side.

Please check my code.

PHP CURL (Server A):

$payload=array();
$payload['a']='123';
$payload['name']='i';
$payload['i']=$base64;
$payload['b']='abc';

$request_headers = array(
                "Content-Type:application/json",
                'Content-length:' . strlen($payload)
            );

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
      curl_setopt($ch, CURLOPT_BUFFERSIZE, 104857640);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
      curl_setopt($ch, CURLOPT_TIMEOUT, 300);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
      curl_setopt($ch, CURLOPT_POSTREDIR, 3);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      $res=curl_exec($ch);

$base64 is php variable, it contains string of 7mb(dynamic value).

Laravel Controller (POST method api):

$a=$req->a;
$b=$req->b;
$name=$req->name;
$i=$req->i;

        if($a=='' || $b=='')
        {
           
            $arr["req"]=  $request->all();
            $data = $request->getContent();
            $arr["data"] = $data;
            $arr["headers"]=$request->headers->all();
            return response()->json($arr); 
        }

$request->all()

Returns empty

$request->getContent();

Returns base64 sting only 1.1mb.

I tried also 'expect:' into curl header, but no luck.

PHP limitation also increased:

   php_value max_execution_time 300
   php_value max_input_time 600
   php_value memory_limit 400M
   php_value post_max_size 400M
   php_value upload_max_filesize 400M
1 Answers

Looks like a php limitation.

Run

ini_get('post_max_size');

If it's less that the needed, then run

ini_set('post_max_size', '32');

Same for files.

ini_get('upload_max_filesize ');

Then

ini_set('upload_max_filesize ', '30M');

Some servers don't allow ini_set(), in that case you will have to modify though php.ini or the hosting settings.

***** Note that post size needs to be always greater than upload size.

Related