配置Epos Now Webhook触发WooCommerce动作遇阻求助
Hey there, let's work through this Epos Now Webhook issue together—you're definitely missing a few key basics here, so let's break it down step by step!
First, Fix How You're Retrieving the Webhook Data
Your biggest mistake right now is how you're trying to get the incoming request data:
file_get_contents('URL here')is wrong—this tells PHP to send a request to that URL, not read the data that's being sent to your endpoint by Epos Now.http_parse_headers()is meant to parse a pre-existing string of headers, not grab the headers from the current incoming request.
Correct Way to Get the Raw Request Body
Epos Now sends JSON data via POST, so use php://input to grab the raw payload, then decode it:
// Get the raw JSON payload from Epos Now $raw_payload = file_get_contents('php://input'); // Convert JSON to a usable PHP array $webhook_data = json_decode($raw_payload, true); // Debug: Write data to a log file (so you can see if it's coming through) // Use ABSPATH for WordPress to ensure the log is saved in your site root file_put_contents(ABSPATH . '/epos-webhook-debug.log', print_r($webhook_data, true) . "\n\n", FILE_APPEND);
Correct Way to Get Epos Now's Request Headers
To access the headers Epos Now sends (like their signature or content type), use one of these methods depending on your server:
// Get all incoming request headers if (function_exists('getallheaders')) { // Works for Apache servers $headers = getallheaders(); } else { // Fallback for Nginx/other servers $headers = []; foreach ($_SERVER as $key => $value) { if (str_starts_with($key, 'HTTP_')) { // Convert to standard header format (e.g., HTTP_X_EPOSNOW_SIGNATURE → X-EposNow-Signature) $header_name = str_replace('_', '-', ucwords(strtolower(substr($key, 5)))); $headers[$header_name] = $value; } } } // Add headers to your debug log file_put_contents(ABSPATH . '/epos-webhook-debug.log', print_r($headers, true) . "\n\n", FILE_APPEND);
Use a WordPress-Friendly Endpoint
If you're adding this code directly to a standalone PHP file, WordPress's security rules or plugins might be blocking the request. Instead, use WordPress's REST API to create a proper, supported endpoint:
Add this to your theme's functions.php file or a custom plugin:
// Register a custom REST endpoint for Epos Now Webhooks add_action('rest_api_init', function() { register_rest_route( 'eposnow/v1', '/webhook', [ 'methods' => 'POST', 'callback' => 'handle_eposnow_webhook', // Note: Add proper security (like signature verification) here later! 'permission_callback' => '__return_true' ] ); }); function handle_eposnow_webhook(WP_REST_Request $request) { // Get raw payload via WordPress's request object $raw_payload = $request->get_body(); $webhook_data = json_decode($raw_payload, true); // Get headers via the request object $headers = $request->get_headers(); // Debug logging file_put_contents(ABSPATH . '/epos-webhook-debug.log', print_r($webhook_data, true) . "\n" . print_r($headers, true) . "\n\n", FILE_APPEND); // Add your WooCommerce logic here (e.g., create orders, update inventory) // Return a 200 OK response to Epos Now return new WP_REST_Response('Webhook received successfully', 200); }
Your Webhook URL will now be https://your-woocommerce-site.com/wp-json/eposnow/v1/webhook—this is a safe, WordPress-approved endpoint that won't get blocked.
Check for Common Pitfalls
- Security Plugins: Tools like Wordfence or Sucuri might block unknown POST requests. Temporarily disable them to test, or add your endpoint to their whitelist.
- SSL Requirement: Epos Now likely requires your endpoint to use HTTPS. Make sure your site has a valid SSL certificate.
- Signature Verification: Don't skip this! Epos Now should send a signature header (check their docs) to prove the request is legitimate. Add code to verify it using your Webhook secret key to prevent malicious requests.
- Debugging Tips: Don't rely on
echo—Epos Now won't see that output. Use the log file method above to confirm you're actually receiving data.
Once you set this up, check the epos-webhook-debug.log file in your site root. You should see the JSON data and headers from Epos Now, which means you can start building your WooCommerce logic on top of it.
内容的提问来源于stack exchange,提问作者Eloise

