Skip to Content

remote_get()

Get a remote resource and cache the hostname at the same time to prevent needing to manually set transients or duplicate lookups.

PHP May 8, 2017

Usage

PHP
nebula()->remote_get($url, $args)

Parameters

$url
(Required) (String) The URL of the requested resource
Default: None

$args
(Optional) (Array) Parameters for wp_remote_get()
Default: None

Parameter Notes

For documentation on the $args parameter, see the WordPress documentation for wp_remote_get().

Request or provide clarification »

Additional Notes

This is basically wp_remote_get() with a caching aspect. If a resource is unavailable, Nebula will not remotely request from that hostname for 10 minutes to maintain quick load times.

Source File

Located in /libs/Utilities/Utilities.php on line 902.

No Hooks

This function does not have any filters or actions available. Request one?
PHP
        public function remote_get($url, $args=null){
            $timer_name = str_replace(array('.', '/'), '_', $this->url_components('filename', $url));
            $timer_name = $this->timer('Remote Get (' . $timer_name . ')', 'start', 'Remote Get');

            //Must be a valid URL
            if ( empty($url) || strpos($url, 'http') !== 0 ){
                return new WP_Error('broke', 'Requested URL is either empty or missing acceptable protocol.');
            }

            $hostname = str_replace('.', '_', $this->url_components('hostname', $url));

            //Check if the resource was unavailable in the last 10 minutes
            if ( !$this->is_available($url, true, false) ){
                $this->timer($timer_name, 'end');
                return new WP_Error('unavailable', 'This resource was unavailable within the last 10 minutes.');
            }

            //Get the remote resource
            $response = wp_safe_remote_get($url, $args);
            if ( is_wp_error($response) ){
                set_transient('nebula_site_available_' . $hostname, 'Unavailable', MINUTE_IN_SECONDS*10); //10 minute expiration
            }

            //Return the response
            set_transient('nebula_site_available_' . $hostname, 'Available', MINUTE_IN_SECONDS*20); //20 minute expiration
            $this->timer($timer_name, 'end');
            return $response;
        }

Override

This function can not be short-circuited with an override filter. Request one?