Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_remote_get() not passing authentication

I've been trying to use wp_remote_get for the first time and having some troubles.

So far I'm testing the response with this code:

<?php 
                    $url      = "https://webapieu3.filefinderanywhere.com/SeussConsultingTestWebportAPI/xml/AllActive";
                    $username = 'username';
                    $password = 'password';
                    $headers = array( 'Authorization' => 'Basic ' . base64_encode( "$username:$password" ), "sslverify" => false );
                    $response = wp_remote_get($url, $headers);
                    
                    if (is_wp_error($response)){
                        echo $response->get_error_message();
                    }else{
                        print_r($response);
                    }

                 ?>

Prior to adding the sslverify param, is_wp_error returned true, but now it's returning false, however I'm getting the following response:

Array ( 
    [headers] => Array ( 
        [cache-control] => no-cache
        [pragma] => no-cache
        [content-type] => application/json; charset=utf-8
        [expires] => -1
        [server] => Microsoft-IIS/7.5
        [www-authenticate] => Basic
        [x-aspnet-version] => 4.0.30319
        [x-powered-by] => ASP.NET
        [date] => Mon, 30 Nov 2015 00:56:09 GMT
        [connection] => close [content-length] => 61 )
    
    [body] => {"Message":"Authorization has been denied for this request."}
    [response] => Array ( 
        [code] => 401
        [message] => Unauthorized )
    
    [cookies] => Array ( )
    [filename] => )

Why is there a 401 error after I've already passed authentication? And how do I pass authentication?

like image 766
milkman15 Avatar asked Nov 30 '15 01:11

milkman15


1 Answers

$wp_request_headers = array(
    'Authorization' => 'Basic ' . base64_encode( 'username:password' )
);

$wp_request_url = 'http://localserver/wordpress-api/wp-json/wp/v2/posts/52';

$wp_delete_post_response = wp_remote_request(
    $wp_request_url,
    array(
        'method'    => 'DELETE',
        'headers'   => $wp_request_headers
    )
);

echo wp_remote_retrieve_response_code( $wp_delete_post_response ) . ' ' . wp_remote_retrieve_response_message( $wp_delete_post_response );
like image 190
mujuonly Avatar answered Oct 23 '22 02:10

mujuonly