0

I need to send a user id to a remote server to download an XML file. I accomplish this with the snippet below:

function custom_file_download($url, $type = 'xml'){

    $ch = curl_init();

    $userData = array( 'user_id' => 'MY_USER_ID' );

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $userData);


    $result = curl_exec($ch);
    if (curl_errno($ch)) {
            exit('Error:' . curl_error($ch));
    }
    curl_close ($ch);

    $uploads = wp_upload_dir();
    $filename = $uploads['basedir'] . '/' . strtok(basename($url), "?") . '.' . $type;

    if (file_exists($filename)){
            @unlink($filename);
    }
    file_put_contents($filename, $result);
    return str_replace($uploads['basedir'], $uploads['baseurl'], $filename);
}

I then use the 'Download from URL' option with the address

[custom_file_download("MY_REMOTE_URL", "xml")]

This successfully downloads the file to /wp-content/uploads. However, WP-Import returns the result "There's a problem with your import file. Please verify that the URL returns a valid import file."

I can import the file manually using the 'upload a file' option with no issues, which suggests there is nothing wrong with the file itself. But I can't create a cron job to complete a daily update until WP All Import can recognise that the file has been created successfully when using 'Download from URL'.

It's been suggested that the file location is not being returned to WP All Import correctly. Please, can someone assist with debugging and/or identifying errors in the snippet above?

Perhaps the download location should be amended to the WP All Import default (/wp-content/uploads/wpallimport/uploads)?

Thanks in advance

4
  • What is custom_file_download doing in square brackets? WP All Import is expecting a URL but it looks like you’re trying to pass a PHP function, apparently with a shortcode, but that’s not how shortcodes work and I’m pretty sure the plugin isn’t even going to process a shortcode. Commented Mar 8, 2018 at 15:59
  • I understand your point, but the file download does work, so I'm not sure this is the issue
    – Stradders
    Commented Mar 8, 2018 at 18:11
  • You don't understand my point. You can download the file manually, but the plugin can't, because [custom_file_download("MY_REMOTE_URL", "xml")] is not going to return a URL, and even if it did, the plugin needs an actual URL not a shortcode of function that returns one. Commented Mar 9, 2018 at 0:07
  • Apologies, I think I've not been clear enough. When I say, manually, I mean I use the plugin to download the file, so the snippet and [url] are working to do this. The manual part is to load the file in to WP All Import. As I said originally, the file is being downloaded by WPAI from the remote host, as expected. However, WPAI doesn't seem to recognise that this has happened successfully.
    – Stradders
    Commented Mar 10, 2018 at 14:17

1 Answer 1

0

Instead of custom curl HTTP request I'll recommend you to use the wp_remote_post() or wp_remote_get() which internally call the HTTP request and handle all possible errors.

Also use download_url() to download the file and media_handle_sideload() to upload the downloaded file into uploads directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.