Cannot upload file to Wufoo using their REST API

298 views Asked by At

On a website I build we're using Wufoo forms to centralize data. So I have a trivial form that I have to submit to Wufoo, using their rest API.

I did everything according to their documentation, but I still get an error when sending the POST request to their endpoint.

This is how my request looks like:

$ref = curl_init('https://{domain}.wufoo.com/api/v3/forms/' . WUFOO_FORM_HASH . '/entries.json');
curl_setopt($ref, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
curl_setopt($ref, CURLOPT_POST, true);
curl_setopt($ref, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ref, CURLOPT_POSTFIELDS, $_formData);
curl_setopt($ref, CURLOPT_USERPWD, WUFOO_API_KEY . ':X');
curl_setopt($ref, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ref, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ref, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ref, CURLOPT_FOLLOWLOCATION, true);
$result = json_decode(curl_exec($ref), true);

And this is the $_formData array:

Array
(
    [Field4] => firstname
    [Field3] => lastname
    [Field5] => [email protected]
    [Field110] => @/var/tmp/random_name/test.txt
)

Whenever I attach a file to the request in Field110, the request fails with this as an error:

Array
(
    [Success] => 0
    [ErrorText] => Errors have been <b>highlighted</b> below.
    [FieldErrors] => Array
        (
            [0] => Array
                (
                    [ID] => Field110
                    [ErrorText] => This file was NOT successfully uploaded. Please try again.
                )

        )

)

Anyone got this problem before and know any solutions? I have to mention that I MUST use the Wufoo rest API to submit this.

2

There are 2 answers

1
Aviram Gabay On

According to the error message the file was not properly successfully uploaded. Meaning there is an issue either with the path, size or type of the file.

Max size for a file is 10MB and for all the fields in a single form is 20MB combined.

Why dont you use the Wufoo API PHP Wrapper it allows among other things to post entries with files....

0
friendofdog On

The value for [Field110] should be a CURL file object.

I found a solution on Github (https://github.com/wufoo/Wufoo-PHP-API-Wrapper/pull/10/commits/094d64436cee405739f5b39bc28f745afed65eb3), which includes the following line:

$curl_file = curl_file_create($this->value, mime_content_type($this->value), pathinfo($this->value, PATHINFO_BASENAME));

You might have to replace $this->value, just make sure it gets the value /var/tmp/random_name/test.txt and pass $curl_file to [Field110].

If you want to see another illustration, I've actually implemented this in a WordPress plugin: https://github.com/friendofdog/better_wfi_wordpress/blob/master/front/post-intermediary.php. Look around line 77.