Filter uncanny-automator

http_request_timeout

Filters the timeout duration for HTTP requests, allowing modification of how long WordPress waits for a response.

add_filter( 'http_request_timeout', $callback, 10, 2 );

Description

Filters the timeout value for WordPress HTTP requests. Developers can use this hook to adjust the default timeout, allowing for longer or shorter waits when fetching data from external URLs. Be mindful that excessively long timeouts can impact site performance and user experience.


Usage

add_filter( 'http_request_timeout', 'your_function_name', 10, 2 );

Parameters

$this (mixed)
This parameter is a reference to the object making the HTTP request, allowing it to access and modify its own properties or methods related to the request.
$url (mixed)
This parameter is a reference to the current object of the `Xml_To_Json_Converter` class, which is responsible for handling XML to JSON conversion.

Return Value

The filtered value.


Examples

add_filter( 'http_request_timeout', 'my_custom_http_request_timeout', 10, 2 );

/**
 * Increase the HTTP request timeout for specific URLs.
 *
 * This function modifies the default WordPress HTTP request timeout,
 * increasing it to 60 seconds for requests made to URLs containing
 * 'example.com/api/data' to allow for potentially longer data fetching.
 *
 * @param int    $timeout The current timeout value in seconds.
 * @param string $url     The URL of the HTTP request.
 * @return int The modified timeout value.
 */
function my_custom_http_request_timeout( $timeout, $url ) {
	// Check if the URL contains a specific pattern that requires a longer timeout.
	if ( strpos( $url, 'example.com/api/data' ) !== false ) {
		// Increase the timeout to 60 seconds for this specific URL.
		return 60;
	}

	// For all other URLs, return the original timeout.
	return $timeout;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/services/loopable/data-integrations/xml-to-json-converter.php:59
uncanny-automator-pro/src/integrations/plugin-actions/triggers/run-add-action.php:221

public function load_from_url( $url ) {

		// Overwrite the default WordPress HTTP agent because some feed doesnt like the syntax and its breaking due to multiple backslashes, or special characters';
		$this->http_request_user_agent = 'WordPress/' . wp_get_wp_version() . ';' . urlencode( get_bloginfo( 'url' ) );

		$args = array(
			'timeout'    => apply_filters( 'http_request_timeout', $this->http_request_timeout, $url ),
			'user-agent' => $this->http_request_user_agent,
		);

		$response = wp_remote_get( $url, $args );
		if ( is_wp_error( $response ) ) {
			throw new RuntimeException(
				sprintf(
				/* translators: %s: Error message */
					esc_html__( 'Failed to fetch the XML from the provided URL. %s', 'uncanny-automator' ),
					esc_html( $response->get_error_message() )
				)
			);
		}

		$xml_data = wp_remote_retrieve_body( $response );
		if ( empty( $xml_data ) ) {
			throw new RuntimeException(
				sprintf(
				/* translators: %s: URL */
					esc_html__( 'No content found in the provided URL: %s', 'uncanny-automator' ),
					esc_url( $url )
				)
			);
		}

		$this->xml_content = $xml_data;

		return $this;
	}


Scroll to Top