0

I'm using the following command to update a database (the line breaks are vital):

curl -X PUT -H 'Content-Type: multipart/form-data; boundary=myboundary' -d '--myboundary Content-ID: <request>

{"jsonKey":"jsonValue"} --myboundary-' 'http://target.url.com/path/to/folder'

This works correctly, but the PHP equivalent has problems:

$data = '--myboundary Content-ID: <request>

{"jsonKey":"jsonValue"} --myboundary-';

    $handle = curl_init();
    curl_setopt_array(
        $handle, 
        array(
            CURLOPT_URL => 'http://target.url.com/path/to/folder',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => 'PUT',
            CURLOPT_HTTPHEADER => 'Content-Type: multipart/form-data; boundary=myboundary',
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_POSTFIELDS => $data
         )
    );
    $response = curl_exec($handle);
    if($response === false){
        echo 'curl error: ' . curl_error($handle);
    }
    curl_close($handle);
    return $response;

It returns no error, but the data entered as json is set to null. I've checked everything else, but can't seem to find clear information on how cURL processes line breaks, and assume there is some difference between PHP breaks and console breaks.

Any insight into the issue of line breaks, or why this may not be working would be appreciated.

And in case anyone is thinking the obvious: no, I can't alter the database, or even view it (work regulations), and this needs to run from PHP (same reason).

DFoster
  • 1
  • 2
  • 1
    This question/answer may help you regarding how to send POST variables within cURL: http://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields – Jasper Sep 16 '14 at 19:15
  • Have you tried `$data = '--myboundary Content-ID: \n\n"jsonKey":"jsonValue"} --myboundary-';` or the like? – JimmyB Sep 17 '14 at 13:32
  • I did read through that question/answer. Alas, it did not correct my problem. Also, I did try replacing the line breaks with \n. I also tried the \r method (as well as combinations of the two), to no avail. I'd thought that would take care of it... In any case, thanks. – DFoster Sep 17 '14 at 17:05

1 Answers1

0

In the first example, you're using POST. In the second example, you're using PUT. Are you sure that's right?

            CURLOPT_CUSTOMREQUEST => 'PUT',
romulusnr
  • 111
  • 4
  • Should have looked over that more carefully... The mistake is in the first command, it should say "PUT" as well. Made the edit (thanks for spotting that). – DFoster Sep 16 '14 at 20:21