I am setting up WooCommerce to trigger a webhook upon order creation. I have set the webhook's URL as the url of a local web API we have setup for this purpose:
http://localhost:3000/store/orders/new
WooCommerce does trigger the webhook with the correct data payload, but the request is reported as failed in the webhook's logs:
Status: HTTP http_request_failed Es wurde keine gültige URL übermittelt.: Array
which, translated from German, means "no valid URL was passed"
I then later changed the URL to a web-facing deployment of our API (https://xxx.azurewebsites.net/store/order/new), and the webhook was received without any problems by the API.
I am not sure if WC webhook's don't play nice with urls that have a custom port (in my case, port 3000), so I wanted to ask the question on whether this is true and if there is a way to make WC webhooks play nice with a localhost development environment.
You can add a webhook by clicking the Add webhook button and filling out a simple form. You can choose an action and the fields associated with that action to be posted to the URL. The URL will receive an HTTP POST request when the selected action fires.
For WooCommerce version 3.3.5, Bjorn's solution doesn't seem to work.
In my case, I had to eddit the file wp-content/plugins/woocommerce/includes/class-wc-webhook.php:185
, method deliver
.
Comment the wp_safe_remote_request
method, and instead, use the insecure http request method.
// Webhook away!
$http_call = _wp_http_get_object();
$response = $http_call->request( $this->get_delivery_url(), $http_args );
//$response = wp_safe_remote_request( $this->get_delivery_url(), $http_args );
It is not the custom port, but localhost.
There is a check in wp-includes/http.php wp_http_validate_url() that rejects all kinds of local IPs unless specifically allowed with a filter on http_request_host_is_external that returns true...
Cheers, Björn
I solved it after inserting the following code at the end of the file: wp-content/themes/MYTHEME/functions.php
function allow_unsafe_urls ( $args ) {
$args['reject_unsafe_urls'] = false;
return $args;
} ;
add_filter( 'http_request_args', 'allow_unsafe_urls' );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With