wp_remote_retrieve_headers example

if ( false == $file_path )
        $options['method'] = 'HEAD';
    else
        $options['method'] = 'GET';

    $response = wp_safe_remote_request( $url$options );

    if ( is_wp_error( $response ) )
        return false;

    $headers = wp_remote_retrieve_headers( $response );
    $headers['response'] = wp_remote_retrieve_response_code( $response );

    // WP_HTTP no longer follows redirects for HEAD requests.     if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {
        return wp_get_http( $headers['location']$file_path, ++$red );
    }

    if ( false == $file_path )
        return $headers;

    // GET request - write it to the supplied filename.
$resp = new stdClass;
        $resp->status = 500;
        $resp->response_code = 500;
        $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility         return $resp;
    }

    // Snoopy returns headers unprocessed.     // Also note, WP_HTTP lowercases all keys, Snoopy did not.     $return_headers = array();
    foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
        if ( !is_array($value) ) {
            $return_headers[] = "$key: $value";
        } else {
            foreach ( $value as $v )
                $return_headers[] = "$key: $v";
        }
    }

    $response = new stdClass;
    $response->status = wp_remote_retrieve_response_code( $resp );
    $response->response_code = wp_remote_retrieve_response_code( $resp );
    
function wp_get_http_headers( $url$deprecated = false ) {
    if ( ! empty( $deprecated ) ) {
        _deprecated_argument( __FUNCTION__, '2.7.0' );
    }

    $response = wp_safe_remote_head( $url );

    if ( is_wp_error( $response ) ) {
        return false;
    }

    return wp_remote_retrieve_headers( $response );
}

/** * Determines whether the publish date of the current post in the loop is different * from the publish date of the previous post in the loop. * * For more information on this and similar theme functions, check out * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ * Conditional Tags} article in the Theme Developer Handbook. * * @since 0.71 * * @global string $currentday The day of the current post in the loop. * @global string $previousday The day of the previous post in the loop. * * @return int 1 when new day, 0 if not a new day. */
if ( SIMPLEPIE_USERAGENT !== $this->useragent ) { // Use default WP user agent unless custom has been specified.                 $args['user-agent'] = $this->useragent;
            }

            $res = wp_safe_remote_request( $url$args );

            if ( is_wp_error( $res ) ) {
                $this->error   = 'WP HTTP Error: ' . $res->get_error_message();
                $this->success = false;

            } else {
                $this->headers = wp_remote_retrieve_headers( $res );

                /* * SimplePie expects multiple headers to be stored as a comma-separated string, * but `wp_remote_retrieve_headers()` returns them as an array, so they need * to be converted. * * The only exception to that is the `content-type` header, which should ignore * any previous values and only use the last one. * * @see SimplePie_HTTP_Parser::new_line(). */
                
Home | Imprint | This part of the site doesn't use cookies.