On Plugin Activation, How Do I Check for Proper Transport Mechanism?

For a plugin that communicates with somewhere else, I hear we’re supposed to use wp_remote_post and wp_remote_get instead of PHP’s Curl library.

Okay, but is there a fast and proper way on plugin execution to test for a blog’s PHP/host OS to have the proper transport mechanism installed? I mean, I could attempt a test POST, but thought WP might have a better, faster mechanism that I can test for? Doing the following is unacceptable:

if (!function_exists('curl_exec')) {
    wp_die('The CURL API is not installed with PHP. You cannot use this plugin without that. Ask your web hosting provider to install it.');
}

Related posts

Leave a Reply

1 comment

  1. I wouldn’t cause the plugin to die like that. Just check for cURL each time you need to make a call or fall back on wp_remote_(post|get) (eg. write a wrapper function that takes care of the check and send the data/headers you want).

    BUT, if you really really want to disable the plugin if cURL is not installed, you can use an activation hook to check for the curl_exec function and deactivate the plugin if not.

    <?php
    register_activation_hook(__FILE__, 'wpse51312_activation');
    function wpse51312_activation()
    {
        if(!function_exists('curl_exec'))
        {
            // Deactivate the plugin
            deactivate_plugins(__FILE__);
    
            // Show the error page, Maybe this shouldn't happen?
            wp_die(
                __('You must enable cURL support to use INSERT PLUGIN NAME'),
                __('Error')
            );
        }
    }
    

    For what it’s worth, I believe that the HTTP api tries to use cURL if it’s available.