1

I'm trying to send my file, but using GET not POST with curl.

I have:

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "http://localhost/test.php/?file_contents=$file");

    $result = curl_exec($ch);

I know that I can easily send the file with a POST curl request like so (taken from this question here):

$data = array(
'uploaded_file' => '@'.$tmpfile.';filename='.$filename,
);

$ch = curl_init();   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

However, I don't want my request to be a POST. I can't seem to figure out how to incorporate a file transfer without it.

Any help or recommendations? I thought of using curl_file_create() but I'm not sure how to implement it in this as the documentation for it is rather terrible.

DJSweetness
  • 153
  • 1
  • 14
  • 3
    `I don't want my request to be a POST.` What? Why? What is your usecase for trying to use GET? – Jonnix Feb 27 '18 at 15:51
  • agree with @JonStirling not using post for this task would be not advisable – treyBake Feb 27 '18 at 15:52
  • 1
    It can't be GET because GET doesn't really support a body (I mean you can send one but a webserver can just reject it just because). It can be PUT though. – apokryfos Feb 27 '18 at 15:52
  • Well, I know that it should be POST, but my old server I'm updating had it as $_GET and instead of doing it the smart way and just changing it to $_POST, I was trying to find a way to do it with GET. Curl does not have really any support for it and it's probably for good reason. – DJSweetness Feb 27 '18 at 15:56

1 Answers1

2
curl_setopt($ch, CURLOPT_URL, "http://localhost/test.php/?".http_build_query(array(
    'name'=>basename($file),
    'file_contents'=>file_get_contents($file)
)));

urlencoding is binary safe, so you can send any file contents this way, however, for big files, url encoding is very wasteful compared to multipart/form-data, the overhead per non-ascii byte (and even some ascii bytes, like 0x20) is 300% (for example, a NULL byte is encoded as %00 , using 3 bytes, where multipart/form-data would only use 1 byte. a 10 MB binary file upload becomes ~30MB , where multipart/form-data would use 10 MB) - so don't do this if you're uploading big files, or if bandwidth is expensive. you should also keep in mind the very limted max url length limitation present by default in many popular web servers (like nginx and apache), and how most web servers log the full url, so your access log files will grow very large, containing every file uploaded this way

hanshenrik
  • 19,904
  • 4
  • 43
  • 89