我第一次尝试使用wp_remote_get,但遇到了一些问题。
到目前为止,我使用以下代码测试响应:

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

                 ?>

在添加sslverify参数之前,i s wp_error返回true,但现在它返回false,但是我得到了以下响应:
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] => )

为什么在我已经通过身份验证之后出现401错误?如何通过身份验证?

最佳答案

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

关于wordpress - wp_remote_get()未通过身份验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33990046/

10-11 05:07