0

I have php scripts to migrate data from one database do another. In one script I call one another to Get the data from the original database, and then it sends it to another. Something like this:

migrateData.php <---> getData.php and then migrateData.php <---> putData.php

In putData.php im using $_GET, but when its too many Data I get the error saying "The length of the requested URL exceeds the capacity limit of this server". I've seen people saying to use $_POST, but i'm not really sure about it. My migrateData.php:

    <?php

  echo "Migration"; 
  $url = "http://localhost/mig/getData.php";
  $client = curl_init($url);
  curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
  $response = curl_exec($client);
  echo "<br>";
  echo "<br>";
  echo $response; 

  $url = "http://localhost/mig/putData.php?data=".$response;
  $url = str_replace ( ' ', '%20', $url);
  $client = curl_init($url);
  curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
  $response = curl_exec($client);
  echo $response;

?>

Any help would be appreciated

2 Answers2

1

Yes when you send a get request to the server (apache, nginx), it may limit the allowed size of a client's HTTP request-line (e.g. ) and the HTTP request header field size.

For example if you have access to the server and its an Apache server you can increase the Apache limits for limit request line and limit request field size.

In case of nginx, you can increase the large_client_header_buffers parameter in nginx.conf.

Using POST method will send the fields outside of the client's HTTP request-line, effectively bypassing the server constraint. Actually POST request are also limited but the default size is bigger.

Using POST or PUT method is recommended instead of changing the server config.

<?php
//Your code to get the response here
// ...

$data = ['data' => $response];

// Prepare new cURL resource
$ch = curl_init('http://localhost/mig/putData.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Submit the POST request
$result = curl_exec($ch);

// Close cURL session handle
curl_close($ch);

//Output the result
echo $result;

?>
filipe
  • 1,957
  • 1
  • 10
  • 23
0

Apache's max $_GET request length is 8190 bytes (https://stackoverflow.com/a/1289611/575047) so trying to send heaps of data in a GET request is a bad idea because it might get cut off.

POST is ideal. By default, apache sets post_max_size to 8mb I believe.

To POST, you just need to add these lines I believe to your putData curl

curl_setopt($client, CURLOPT_POST, 1);
curl_setopt($client, CURLOPT_POSTFIELDS, $response);
Benno
  • 3,008
  • 3
  • 26
  • 41