2

I have a PHP (Laravel framework) application and when a request is being made on my site, i want to do the exact same request with CURL but only change the URL. That includes:

  • headers
  • POST data (if present)
  • uploaded files (if present)
  • anything else a Request would contain

I have started writing my own code like this:

<?php
$url = $request->fullUrl(); // Current URL

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "http://otherurlhere.com/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->method());

if ($request->method() == "POST") {
  $fields_string = "";
  $fields = $request->all();

  curl_setopt($ch,CURLOPT_POST, true);

  if (count($request->files)) {
    foreach ($_FILES as $key => $file) {
      if (is_array($file['name'])) {
        foreach ($file['name'] as $key2 => $value2) {
          $tmpfile = $_FILES[$key]['tmp_name'][$key2];
          $filename = basename($_FILES[$key]['name'][$key2]);
          $fields[$key] = '@'.$tmpfile.';filename='.$filename;
        }
      }
      else {
        $tmpfile = $_FILES[$key]['tmp_name'];
        $filename = basename($_FILES[$key]['name']);
        $fields[$key] = '@'.$tmpfile.';filename='.$filename;
      }
    }
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
  }
  else {
    $fields_string = http_build_query($fields);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  }
}

$headers = [];
foreach ($request->headers->all() as $key => $header) {
  $headers = [];
  foreach ($request->headers->all() as $key => $header) {
    $headers[] = $key.": ".$header[0];
  }

  if (count($headers) > 0) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

$headers = explode("\n", $header);

foreach ($headers as $header) {
  if (!empty($header))
    if (substr($header, 0, 12) == "Content-Type")
      header($header);
}


echo $body;

//close connection
curl_close($ch);

exit();
?>

But if feels like i am complicating things too much, plus the files upload part isn't really working, so i was wondering if anybody new of a very simple solution that would just copy everything in a PHP CURL replica.

Even if it is the same as "Chrome copy as CURL" functionality and it would give me a command. That would be great as well, because i could use exec() with that.

  • You have a site with a function to upload files and you want to replicate the complete request to a different URL. Can you explain what you want to accomplish with that? Perhaps there is a different sort of solution. – Kwebble Sep 23 '16 at 15:32
  • Our company's developers are working on internal virtual machines and we need to be able to intercept all their requests to only allow approved websites based on our enterprise policy. Approved requests, we pass them on to the actual site.. Unapproved requests, we block them and show them an error message. – Alexandru Contes Sep 23 '16 at 15:40
  • Isn't it easier to run all traffic through a proxy server and configure that to only allow access to approved sites? – Kwebble Sep 23 '16 at 15:43
  • Good idea. Do you know of such a proxy server that would allow configuration such as maximum file size allowed when uploading, or only approving GET for a site (disable POST), etc..? – Alexandru Contes Sep 23 '16 at 15:48
  • No, sorry. I've used Apache as reversed proxy but nothing for filtering etc. Perhaps this Wikipedia article https://en.wikipedia.org/wiki/Proxy_server#Monitoring_and_filtering can help you search for the right keywords. – Kwebble Sep 23 '16 at 15:56
  • Thanks for suggestion, but looks like what you can do with a proxy server is whitelist or ban sites, nothing else.. I am looking for more control and filters. – Alexandru Contes Sep 23 '16 at 16:17
  • Shouldn't you just use a firewall either on the VM for this or a hardware firewall? – drew010 Sep 23 '16 at 21:42

0 Answers0