• Resolved therelo

    (@therelo)


    Hello! I’ve searched the forum and internet for a way to send files through wp_remote_post after an uploaded file is submitted through Forminator, but couldn’t find a solution to my problem.

    The problem at hand is:
    I need to send an image/pdf from my wordpress site to a company API. When a user submits the form I am using wp_remote_post to send some data in JSON. The PHP code snippet is a representation of what I am sending currently.

    $response = wp_remote_post(
                $target_url,
                array(
                    'method'      => 'POST',
                    'timeout'     => 45,
                    'headers'     => array(),
                    'body'        => json_encode(array(
                        "USER_ID" => $requestData['ACCID'],
                        "EMAIL" => $requestData['EMAIL'],
                        "PHONE" => $phone,
                        "SERIALNUM" => $requestData['SER'],
                        "SEQID" => $entry_id
                    )),
                )
            );

    In addition to this I want to send the file that is in the {upload-1} field of the form. How can I achieve that?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @therelo

    I hope you are doing good today.

    I pinged our SLS Team to review your query. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again @therelo

    This is an example snippet to get the file URL and add it to the API request "FILE" => $file_url,
    This $requestData you will have to manage here and form ID should be changed from 2910 to your form ID.

    add_action( 'forminator_form_after_save_entry', 'wpmudev_get_upload_send_data', 10, 2 );
    function wpmudev_get_upload_send_data( $form_id, $response ) {
    	if ( $response && is_array( $response ) ) {
    		if ( $response['success'] ) {
    			if ( $form_id == 2910 ) {
    				$submitted_data = Forminator_CForm_Front_Action::$prepared_data;
    				if ( ! empty( $submitted_data['upload-1'] ) ) {
    					if( is_array( $submitted_data['upload-1']['file'] ) ){
    						if( ! empty( $submitted_data['upload-1']['file']['file_url'] ) ) {
    							$file_url = $submitted_data['upload-1']['file']['file_url'];
    							$response = wp_remote_post(
    								$target_url,
    								array(
    									'method'      => 'POST',
    									'timeout'     => 45,
    									'headers'     => array(),
    									'body'        => json_encode(array(
    										"USER_ID" => $requestData['ACCID'],
    										"EMAIL" => $requestData['EMAIL'],
    										"PHONE" => $phone,
    										"SERIALNUM" => $requestData['SER'],
    										"SEQID" => $entry_id,
    										"FILE" => $file_url,
    									)),
    								)
    							);
    						}
    					}
    				}
    			}
    		}
    	}
    }

    You can add the above snippet as a mu-plugins. Please check this link on how to implement the above code as a mu-plugins:

    Must Use Plugins

    Kind Regards,
    Kris

    Thread Starter therelo

    (@therelo)

    Hi,
    Thank you for your reply. I believe this is sending the image/file URL through the API. The issue with that is that the company server cannot download the image with curl or get_fp_contents because it has an older php version. In other words I want to be able to send the image/pdf file as content in its entirety through API (Maybe through base64?)

    Is there a way to do so that I am not aware of?

    Thanks in advance for your response.

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @therelo,

    I hope you are doing well today!

    We have forwarded your request to our SLS (Second Line Support) team if there is any workaround that we can provide. Please keep in mind that our SLS Team deals with more complicated issues, so it may take a little longer for us to reply here.

    Thank you for your patience while we look into this further.

    Kind regards,
    Zafer

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @therelo,

    Here below you can find the response from our SLS team

    You will need to replace this line

    $file_url = $submitted_data['upload-1']['file']['file_url'];

    with the following code;

    $file_url = $submitted_data['upload-1']['file']['file_url'];
    $file_content = file_get_contents($file_url);
    $file_encode = base64_encode($file_content);

    and then in place of this "FILE" => $file_url, use "FILE" => $file_encode

    Kind regards,
    Zafer

    Thread Starter therelo

    (@therelo)

    This has managed to work!
    Although I am aware that it is an inelegant solution because of the larger overhead/size in the API, I did not find a better way to do it given the constraints.

    I will now mark it as resolved, thank you!

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @therelo,

    Glad to know the workaround suggested works. Please feel free to open a new ticket if you need any other assistance.

    Kind Regards,
    Nebu John

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Forminator – wp_remote_post an uploaded file on form submit’ is closed to new replies.