Php 在Postman上使用Wordpress Nonce作为setRequestHeader,就像在jQuery/Javascript中一样

Php 在Postman上使用Wordpress Nonce作为setRequestHeader,就像在jQuery/Javascript中一样,php,wordpress,rest,postman,wordpress-rest-api,Php,Wordpress,Rest,Postman,Wordpress Rest Api,这是我的问题 实际上,我似乎在试图解决一个bug,而不是解决一个问题,这就是为什么: 我正在创建一个插件,该插件在WordPress环境中运行良好,我使用WordPress nonce作为身份验证模式 我正在使用jQuery/Ajax在我之前用PHP创建的头中传递nonce: public function my_register_route() { register_rest_route( 'top-list-route', 'my-top-list-get', array(

这是我的问题

实际上,我似乎在试图解决一个bug,而不是解决一个问题,这就是为什么:

我正在创建一个插件,该插件在WordPress环境中运行良好,我使用WordPress nonce作为身份验证模式

我正在使用jQuery/Ajax在我之前用PHP创建的头中传递nonce:

public function my_register_route() {

   register_rest_route( 'top-list-route', 'my-top-list-get', array(
    array(
    'methods'  => WP_REST_Server::READABLE,
    'callback' => array($this, 'my_top_list_get'),
    'permission_callback' => function() {
       return current_user_can( 'edit_posts' );
     },
   ),

) );
PHP:

Javascript/jQuery:

$.ajax({

method: 'GET',
url: wpApiSettings.root+'top-list-route/my-top-list-get',
contentType: 'application/json; charset=utf-8',
beforeSend: function ( xhr ) {
    xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
dataType: 'json',
success: ajaxResponse

});

function ajaxResponse(data) {
    console.log(data)
}
到目前为止,很好,简而言之,这个应用程序将在我之前用PHP创建的这条路线上运行:

public function my_register_route() {

   register_rest_route( 'top-list-route', 'my-top-list-get', array(
    array(
    'methods'  => WP_REST_Server::READABLE,
    'callback' => array($this, 'my_top_list_get'),
    'permission_callback' => function() {
       return current_user_can( 'edit_posts' );
     },
   ),

) );
现在,如果我尝试使用nonce(浏览器上的I console.log)和Postman、visual studio代码(扩展了REST客户端)或仅在chrome上的URL中运行相同的代码,那么它将不起作用,例如:

http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
邮递员:

GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get
(IN THE HEADERS section) X-WP-Nonce (key) 47489127d8 (value, for example)
将返回:

{
    "code": "rest_cookie_invalid_nonce",
    "message": "Cookie nonce is invalid",
    "data": {
        "status": 403
    }
}
HTTP/1.1 403 Forbidden

{
  "code": "rest_cookie_invalid_nonce",
  "message": "Cookie nonce is invalid",
  "data": {
    "status": 403
  }
}
或者,如果我使用此URL,例如:

http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
它将返回相同的状态(403 rest\u cookie\u invalid\u nonce)

visual studio代码也存在同样的问题,如果我将任何头添加到GET请求,例如:

http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
将返回:

{
    "code": "rest_cookie_invalid_nonce",
    "message": "Cookie nonce is invalid",
    "data": {
        "status": 403
    }
}
HTTP/1.1 403 Forbidden

{
  "code": "rest_cookie_invalid_nonce",
  "message": "Cookie nonce is invalid",
  "data": {
    "status": 403
  }
}

有什么提示吗?

这可能有点晚了,但希望是其他人可能需要的解决方案,这就是我在functions.php中传递nonce的方式

wp_localize_script( 'app', 'WP_API_Settings', array(
  'endpoint' => esc_url_raw( rest_url() ),
  'nonce' => wp_create_nonce( 'wp_rest' )
) );
在Postman中,我通过请求头传递nonce

X-WP-Nonce:{{nonce}}
Cookie:{{cookie}}
我注意到,nonce过期了,您必须将新值传递给Postman。我正在使用一个变量,以便它应用于集合中的所有路由。如果获得无效的nonce,请确保使用Chrome Developer Tools F12通过Web登录,并在控制台I键入wpApiSettings.nonce,其中显示最新的nonce值。将该值复制并粘贴到变量中,或使用原始值并应用到标头


我希望这会有帮助,弄明白这一点很痛苦

我想更新这篇文章,我发现了与这篇文章相关的非常有用的信息。下面的链接提供了详细信息,但Postman拦截器允许同步Cookie,以便您可以在Postman中访问它们