php-cross-domain-proxy icon indicating copy to clipboard operation
php-cross-domain-proxy copied to clipboard

Support for multipart/form-data

Open borisaguilar opened this issue 7 years ago • 4 comments

Currently the script replaces the data being sent with nothing :'( I need to POST a form with a File input but the current script doesn't allow it

borisaguilar avatar Aug 18 '16 22:08 borisaguilar

Do you have any example that can reproduce the issue?

On 19 Aug 2016 01:45, "Boris Aramis Aguilar Rodríguez" < [email protected]> wrote:

Currently the script replaces the data being sent with nothing :'( I need to POST a form with a File input but the current script doesn't allow it

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softius/php-cross-domain-proxy/issues/28, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjxiU4mUbpujP09KozFGKtEha4Uo7s6ks5qhOCWgaJpZM4Jn_WP .

softius avatar Aug 19 '16 04:08 softius

I must confirm, sending data with content-type: multipart/form-data doesn't work, for two reasons:

  • The content-type header is passed over including boundary segment (which should be removed, as CURL is supposed to manage it)
  • The $_FILES payload is not appended to CURL request

piotr-cz avatar Jun 23 '17 23:06 piotr-cz

Hi @piotr-cz, I tried the solution suggested by you. I am able to see the uploaded file in /tmp/ directory of our server. When I tried to send the file to the backend service, the call is not reaching the backend service. I added logs for debugging the proxy service, and I didn't get any trace after curl_exec method. All the other API calls are working except file upload.

Elavarasanlee avatar Apr 09 '20 05:04 Elavarasanlee

Code Snippet:

curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);   // (re-)send headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // return response
curl_setopt($ch, CURLOPT_HEADER, true);       // enabled response headers

if(array_key_exists('CONTENT_TYPE', $_SERVER) && strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data;') === 0) {
    debug("Request has Multipart Files: " . print_r($_FILES, true));
    $filesList = $_FILES;
    if(empty($request_params)) {
        $request_params = array();
    }
    foreach ($filesList as $key => $fileProps) {
        $request_params[$key] = new CurlFile($fileProps['tmp_name'], $fileProps['type'], $fileProps['name']);
    }
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    debug("Request Params with CurlFile(s): " . print_r($request_params, true));
}

// add data for POST, PUT or DELETE requests
if ('POST' == $request_method) {
    $post_data = is_array($request_params) ? http_build_query($request_params) : $request_params;
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $post_data);
} elseif ('PUT' == $request_method || 'DELETE' == $request_method) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request_params);
}

// Set multiple options for curl according to configuration
if (is_array($curl_options) && 0 <= count($curl_options)) {
    curl_setopt_array($ch, $curl_options);
}
debug("Initiating CURL $request_method call with data: " . print_r($request_params, true));

// retrieve response (headers and content)
$response = curl_exec($ch);
curl_close($ch);

debug("Response Received from CURL: " . print_r($response, true));

In the above code I am able to see the log trace till: debug("Initiating CURL $request_method call with data: " . print_r($request_params, true));

I need little help to proceed further.

Thank you, Elavarasan M (Lee)

Elavarasanlee avatar Apr 09 '20 05:04 Elavarasanlee