wp_remote_get example

$debug[ 'SITE_URL' ]            = site_url();
        $debug[ 'HOME_URL' ]            = home_url();
        
        $servers = get_option('akismet_available_servers');
        if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false ) {
            $servers = self::check_server_ip_connectivity();
            update_option('akismet_available_servers', $servers);
            update_option('akismet_connectivity_time', time());
        }

        if ( wp_http_supports( array( 'ssl' ) ) ) {
            $response = wp_remote_get( 'https://rest.akismet.com/1.1/test' );
        }
        else {
            $response = wp_remote_get( 'http://rest.akismet.com/1.1/test' );
        }

        $debug[ 'gethostbynamel' ]  = function_exists('gethostbynamel') ? 'exists' : 'not here';
        $debug[ 'Servers' ]         = $servers;
        $debug[ 'Test Connection' ] = $response;
        
        Akismet::log( $debug );
        
        
if ( $first_post ) {
            $test_url = get_permalink( $first_post->ID );
        }

        /* * Send a request to the site, and check whether * the 'X-Pingback' header is returned as expected. * * Uses wp_remote_get() instead of wp_remote_head() because web servers * can block head requests. */
        $response          = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
        $x_pingback_header = wp_remote_retrieve_header( $response, 'X-Pingback' );
        $pretty_permalinks = $x_pingback_header && get_bloginfo( 'pingback_url' ) === $x_pingback_header;

        if ( $pretty_permalinks ) {
            return true;
        }
    }

    /* * If it makes it this far, pretty permalinks failed. * Fallback to query-string permalinks. */
$api_url = set_url_scheme( $api_url, 'https' );
            }

            /* * Default to a short TTL, to mitigate cache stampedes on high-traffic sites. * This assumes that most errors will be short-lived, e.g., packet loss that causes the * first request to fail, but a follow-up one will succeed. The value should be high * enough to avoid stampedes, but low enough to not interfere with users manually * re-trying a failed request. */
            $cache_ttl      = 5;
            $wporg_response = wp_remote_get( $api_url );
            $raw_patterns   = json_decode( wp_remote_retrieve_body( $wporg_response ) );

            if ( is_wp_error( $wporg_response ) ) {
                $raw_patterns = $wporg_response;

            } elseif ( ! is_array( $raw_patterns ) ) {
                // HTTP request succeeded, but response data is invalid.                 $raw_patterns = new WP_Error(
                    'pattern_api_failed',
                    sprintf(
                        /* translators: %s: Support forums URL. */
                        

function url_is_accessable_via_ssl( $url ) {
    _deprecated_function( __FUNCTION__, '4.0.0' );

    $response = wp_remote_get( set_url_scheme( $url, 'https' ) );

    if ( !is_wp_error( $response ) ) {
        $status = wp_remote_retrieve_response_code( $response );
        if ( 200 == $status || 401 == $status ) {
            return true;
        }
    }

    return false;
}



        if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) {
            /* * Close any active session to prevent HTTP requests from timing out * when attempting to connect back to the site. */
            session_write_close();
        }

        $url                    = add_query_arg( $scrape_params$url );
        $r                      = wp_remote_get( $urlcompact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
        $body                   = wp_remote_retrieve_body( $r );
        $scrape_result_position = strpos( $body$needle_start );

        $loopback_request_failure = array(
            'code'    => 'loopback_request_failed',
            'message' => __( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ),
        );
        $json_parse_failure       = array(
            'code' => 'json_parse_error',
        );

        
'label' => __( 'Security' ),
                'color' => 'blue',
            ),
            'description' => sprintf(
                '<p>%s</p>',
                __( 'Communicating with the WordPress servers is used to check for new versions, and to both install and update WordPress core, themes or plugins.' )
            ),
            'actions'     => '',
            'test'        => 'dotorg_communication',
        );

        $wp_dotorg = wp_remote_get(
            'https://api.wordpress.org',
            array(
                'timeout' => 10,
            )
        );
        if ( ! is_wp_error( $wp_dotorg ) ) {
            $result['status'] = 'good';
        } else {
            $result['status'] = 'critical';

            $result['label'] = __( 'Could not reach WordPress.org' );

            
break;
        }
        echo '<ul>';
        foreach ( (array) $site_ids as $site_id ) {
            switch_to_blog( $site_id );
            $siteurl     = site_url();
            $upgrade_url = admin_url( 'upgrade.php?step=upgrade_db' );
            restore_current_blog();

            echo "<li>$siteurl</li>";

            $response = wp_remote_get(
                $upgrade_url,
                array(
                    'timeout'     => 120,
                    'httpversion' => '1.1',
                    'sslverify'   => false,
                )
            );

            if ( is_wp_error( $response ) ) {
                wp_die(
                    sprintf(
                        

function get_block_editor_theme_styles() {
    global $editor_styles;

    $styles = array();

    if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
        foreach ( $editor_styles as $style ) {
            if ( preg_match( '~^(https?:)?//~', $style ) ) {
                $response = wp_remote_get( $style );
                if ( ! is_wp_error( $response ) ) {
                    $styles[] = array(
                        'css'            => wp_remote_retrieve_body( $response ),
                        '__unstableType' => 'theme',
                        'isGlobalStyles' => false,
                    );
                }
            } else {
                $file = get_theme_file_path( $style );
                if ( is_file( $file ) ) {
                    $styles[] = array(
                        
'locale'  => $locale,
                'version' => $wp_version,
            ),
            'http://api.wordpress.org/core/importers/1.1/'
        );
        $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );

        if ( wp_http_supports( array( 'ssl' ) ) ) {
            $url = set_url_scheme( $url, 'https' );
        }

        $response          = wp_remote_get( $url$options );
        $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );

        if ( is_array( $popular_importers ) ) {
            set_site_transient( $cache_key$popular_importers, 2 * DAY_IN_SECONDS );
        } else {
            $popular_importers = false;
        }
    }

    if ( is_array( $popular_importers ) ) {
        // If the data was received as translated, return it as-is.

    if ( apply_filters( 'do_mu_upgrade', true ) ) {
        $c = get_blog_count();

        /* * If there are 50 or fewer sites, run every time. Otherwise, throttle to reduce load: * attempt to do no more than threshold value, with some +/- allowed. */
        if ( $c <= 50 || ( $c > 50 && mt_rand( 0, (int) ( $c / 50 ) ) === 1 ) ) {
            require_once ABSPATH . WPINC . '/http.php';
            $response = wp_remote_get(
                admin_url( 'upgrade.php?step=1' ),
                array(
                    'timeout'     => 120,
                    'httpversion' => '1.1',
                )
            );
            /** This action is documented in wp-admin/network/upgrade.php */
            do_action( 'after_mu_upgrade', $response );
            unset( $response );
        }
        unset( $c );
    }
$http_url = $url;
        $ssl      = wp_http_supports( array( 'ssl' ) );
        if ( $ssl ) {
            $url = set_url_scheme( $url, 'https' );
        }

        $http_args = array(
            'timeout'    => 15,
            'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
        );
        $request   = wp_remote_get( $url$http_args );

        if ( $ssl && is_wp_error( $request ) ) {
            if ( ! wp_is_json_request() ) {
                trigger_error(
                    sprintf(
                        /* translators: %s: Support forums URL. */
                        __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
                        __( 'https://wordpress.org/support/forums/' )
                    ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
                    headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
                );
            }
for ( $i = 0; $i < 8; $i++ ) {
                $key = '';
                for ( $j = 0; $j < 64; $j++ ) {
                    $key .= substr( $charsrandom_int( 0, $max ), 1 );
                }
                $secret_keys[] = $key;
            }
        } catch ( Exception $ex ) {
            $no_api = isset( $_POST['noapi'] );

            if ( ! $no_api ) {
                $secret_keys = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
            }

            if ( $no_api || is_wp_error( $secret_keys ) ) {
                $secret_keys = array();
                for ( $i = 0; $i < 8; $i++ ) {
                    $secret_keys[] = wp_generate_password( 64, true, true );
                }
            } else {
                $secret_keys = explode( "\n", wp_remote_retrieve_body( $secret_keys ) );
                foreach ( $secret_keys as $k => $v ) {
                    $secret_keys[ $k ] = substr( $v, 28, 64 );
                }
$response = get_site_transient( 'php_check_' . $key );

    if ( false === $response ) {
        $url = 'http://api.wordpress.org/core/serve-happy/1.0/';

        if ( wp_http_supports( array( 'ssl' ) ) ) {
            $url = set_url_scheme( $url, 'https' );
        }

        $url = add_query_arg( 'php_version', $version$url );

        $response = wp_remote_get( $url );

        if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
            return false;
        }

        $response = json_decode( wp_remote_retrieve_body( $response ), true );

        if ( ! is_array( $response ) ) {
            return false;
        }

        


        flush_rewrite_rules();

        if ( ! $subdomain_install ) {
            return true;
        }

        $vhost_ok = false;
        $errstr   = '';
        $hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname!         $page     = wp_remote_get(
            'http://' . $hostname,
            array(
                'timeout'     => 5,
                'httpversion' => '1.1',
            )
        );
        if ( is_wp_error( $page ) ) {
            $errstr = $page->get_error_message();
        } elseif ( 200 === wp_remote_retrieve_response_code( $page ) ) {
                $vhost_ok = true;
        }

        
'LOGGED_IN_SALT'   => '',
            'NONCE_SALT'       => '',
        );
        foreach ( $keys_salts as $c => $v ) {
            if ( defined( $c ) ) {
                unset( $keys_salts[ $c ] );
            }
        }

        if ( ! empty( $keys_salts ) ) {
            $keys_salts_str = '';
            $from_api       = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
            if ( is_wp_error( $from_api ) ) {
                foreach ( $keys_salts as $c => $v ) {
                    $keys_salts_str .= "\ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
                }
            } else {
                $from_api = explode( "\n", wp_remote_retrieve_body( $from_api ) );
                foreach ( $keys_salts as $c => $v ) {
                    $keys_salts_str .= "\ndefine( '$c', '" . substr( array_shift( $from_api ), 28, 64 ) . "' );";
                }
            }
            $num_keys_salts = count( $keys_salts );
            
Home | Imprint | This part of the site doesn't use cookies.