wp_safe_remote_request example

if ( $red > 5 )
        return false;

    $options = array();
    $options['redirection'] = 5;

    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 );
    }

    
'redirection' => $this->redirects,
            );

            if ( ! empty( $this->headers ) ) {
                $args['headers'] = $this->headers;
            }

            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(). */
$headers = array();
        $args    = array();
        if ( true === $head ) {
            $args['method'] = 'HEAD';
        }
        if ( ! empty( $username ) && ! empty( $password ) ) {
            $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password);
        }

        $args['headers'] = $headers;

        return wp_safe_remote_request( $url$args );
    }

    /** * Bumps up the request timeout for http requests. * * @param int $val * @return int */
    public function bump_request_timeout( $val ) {
        return 60;
    }

    

function _fetch_remote_file($url$headers = "" ) {
    $resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) );
    if ( is_wp_error($resp) ) {
        $error = array_shift($resp->errors);

        $resp = new stdClass;
        $resp->status = 500;
        $resp->response_code = 500;
        $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility         return $resp;
    }

    // Snoopy returns headers unprocessed.
Home | Imprint | This part of the site doesn't use cookies.