如何在WordPress插件中异步发起多个HTTP请求并兼容多版本?
Absolutely! There are reliable, WordPress-native and low-level ways to fire off 10+ asynchronous HTTP requests without waiting for any responses—compatible with most WordPress versions, both new and old. Let’s break down the best approaches for your plugin:
If you’re targeting WordPress 4.4 or newer (which covers almost all active installations today), the simplest way is to use the built-in wp_remote_request() function with the blocking parameter set to false. This tells WordPress to send the request but immediately move on without waiting for a response.
Example Code:
function my_plugin_async_http_requests() { // List your 10+ target URLs $target_urls = [ 'https://your-api.com/endpoint-1', 'https://your-api.com/endpoint-2', 'https://your-api.com/endpoint-3', // Add more URLs as needed ]; foreach ($target_urls as $url) { wp_remote_request($url, [ 'method' => 'POST', // Use 'GET' if your endpoints expect it 'blocking' => false, // Optional: Add headers, body data, etc. 'headers' => [ 'Content-Type' => 'application/json', 'X-WP-Plugin' => 'My-Custom-Plugin' ], 'body' => json_encode([ 'payload' => 'your-data-here' ]) ]); } }
Notes:
- This relies on WordPress’s underlying HTTP transport (usually cURL or streams). Most modern servers support non-blocking requests, but if not, it’ll fall back to blocking mode (though this is rare).
- No extra dependencies needed—pure WordPress core functionality.
If you need to support older WordPress versions (pre-4.4) or want more low-level control, using cURL’s multi-handle functionality is the way to go. This lets you fire off multiple requests in parallel without waiting for any responses, and works as long as your server has cURL enabled (standard on nearly all WP hosts).
Example Code:
function my_plugin_multi_curl_async_requests() { $target_urls = [ 'https://your-api.com/endpoint-1', 'https://your-api.com/endpoint-2', // Add more URLs ]; // Initialize cURL multi-handle $multi_handle = curl_multi_init(); $curl_handles = []; // Create individual cURL handles for each URL foreach ($target_urls as $index => $url) { $ch = curl_init($url); // Configure request settings curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['payload' => 'your-data'])); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'X-WP-Plugin: My-Custom-Plugin' ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // We don't need the response curl_setopt($ch, CURLOPT_TIMEOUT, 2); // Short timeout since we don't wait curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); $curl_handles[$index] = $ch; curl_multi_add_handle($multi_handle, $ch); } // Execute all asynchronous requests do { $execution_status = curl_multi_exec($multi_handle, $active_handles); if ($active_handles) { // Yield CPU resources instead of blocking curl_multi_select($multi_handle); } } while ($active_handles && $execution_status == CURLM_OK); // Clean up resources foreach ($curl_handles as $ch) { curl_multi_remove_handle($multi_handle, $ch); curl_close($ch); } curl_multi_close($multi_handle); }
Notes:
- This works on all WordPress versions as long as cURL is enabled (which it almost always is).
- You have full control over request timeouts, headers, and other parameters—great for fine-tuning performance.
- Server Limits: Some hosts restrict concurrent HTTP requests. If you’re sending 20+ requests, consider adding a small delay between batches (e.g.,
usleep(10000)for 10ms) to avoid triggering security limits. - No Response Handling: Since you’re not waiting for responses, you won’t know immediately if a request fails. If failure tracking is critical, consider adding logging on the target endpoint, or using a background job system like WP Cron for delayed, non-blocking execution.
- Performance: Even asynchronous requests use server resources. Avoid running this code during high-traffic events (like page loads) unless necessary—trigger it via admin actions, cron jobs, or webhooks instead.
内容的提问来源于stack exchange,提问作者beginner

