WP_REMOTE_POST不使用admin-post.php

时间:2020-07-01 作者:GeeC

我正在开发一个插件,它有一个长期运行的功能,用于设置自定义帖子类型的帖子负载。我想异步运行此函数。为了做到这一点,我尝试了以下方法:

add_action( \'admin_post_my_action\', \'my_long_running_function\' );

$url = admin_url( \'admin-post.php\' );
$args = [
    \'method\'      => \'POST\',
    \'timeout\'     => 50,
    \'redirection\' => 5,
    \'blocking\'    => true,
    \'headers\'     => [] ,
    \'body\'        => [
        \'action\' => \'my_action\'
    ],
    \'cookies\'     => []
];
$response = wp_remote_post( $url, $args );

if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    printf(
        \'Something went wrong: %s\',
        $error_message
    );
} else {
    echo \'Response:<pre>\';
    print_r( $response );
    echo \'</pre>\';
}
请注意,我知道\'blocking\' => true, 它不会异步运行,但我将其更改为异步运行,以便可以看到响应。

在里面my_long_running_function, 结果通过电子邮件发送给网站管理员。我还输出了一些调试消息,在NetBeans中有一个断点。

该函数未通过wp\\u remote\\u post调用,其响应如下:

Array
(
    [headers] => Requests_Utility_CaseInsensitiveDictionary Object
        (
            [data:protected] => Array
                (
                    [date] => Wed, 01 Jul 2020 05:23:22 GMT
                    [server] => Apache/2.4.37 (Win64) PHP/7.2.14
                    [x-powered-by] => PHP/7.2.14
                    [expires] => Wed, 11 Jan 1984 05:00:00 GMT
                    [cache-control] => no-cache, must-revalidate, max-age=0
                    [x-frame-options] => SAMEORIGIN
                    [referrer-policy] => strict-origin-when-cross-origin
                    [content-length] => 0
                    [content-type] => text/html; charset=UTF-8
                )
        )
    [body] => 
    [response] => Array
        (
            [code] => 200
            [message] => OK
        )
    [cookies] => Array
        (
        )
    [filename] => 
    [http_response] => WP_HTTP_Requests_Response Object
        (
            [response:protected] => Requests_Response Object
                (
                    [body] => 
                    [raw] => HTTP/1.1 200 OK
Date: Wed, 01 Jul 2020 05:23:22 GMT
Server: Apache/2.4.37 (Win64) PHP/7.2.14
X-Powered-By: PHP/7.2.14
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
X-Frame-Options: SAMEORIGIN
Referrer-Policy: strict-origin-when-cross-origin
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8
                    [headers] => Requests_Response_Headers Object
                        (
                            [data:protected] => Array
                                (
                                    [date] => Array
                                        (
                                            [0] => Wed, 01 Jul 2020 05:23:22 GMT
                                        )
                                    [server] => Array
                                        (
                                            [0] => Apache/2.4.37 (Win64) PHP/7.2.14
                                        )
                                    [x-powered-by] => Array
                                        (
                                            [0] => PHP/7.2.14
                                        )
                                    [expires] => Array
                                        (
                                            [0] => Wed, 11 Jan 1984 05:00:00 GMT
                                        )
                                    [cache-control] => Array
                                        (
                                            [0] => no-cache, must-revalidate, max-age=0
                                        )
                                    [x-frame-options] => Array
                                        (
                                            [0] => SAMEORIGIN
                                        )
                                    [referrer-policy] => Array
                                        (
                                            [0] => strict-origin-when-cross-origin
                                        )
                                    [content-length] => Array
                                        (
                                            [0] => 0
                                        )
                                    [content-type] => Array
                                        (
                                            [0] => text/html; charset=UTF-8
                                        )
                                )
                        )
                    [status_code] => 200
                    [protocol_version] => 1.1
                    [success] => 1
                    [redirects] => 0
                    [url] => http://my-site/wp-admin/admin-post.php
                    [history] => Array
                        (
                        )
                    [cookies] => Requests_Cookie_Jar Object
                        (
                            [cookies:protected] => Array
                                (
                                )
                        )
                )
            [filename:protected] => 
            [data] => 
            [headers] => 
            [status] => 
        )
)
我已经尝试通过一个url调用它来管理帖子。php:

http://my-site/wp-admin/admin-post.php?action=my_action
这与预期的一样,发送电子邮件并输出调试消息。

鉴于上述请求有效,并且是GET请求,我还尝试调用wp_remote_post 具有\'method\' => \'GET\', 我试过wp_remote_get. 两者都不起作用。

我还浏览了发送请求和处理响应的所有代码。没有任何东西可以帮助指示问题的原因。

非常感谢您对解决此问题的任何帮助,甚至是关于如何调试调用管理帖子的建议。php通过wp_remote_post. 这是我第一次体验wp_remote_post. 或者甚至是异步调用我的函数的替代方法。

1 个回复
最合适的回答,由SO网友:GeeC 整理而成

多亏了Sally CJ的评论,问题在于用户在调用admin post时没有经过身份验证。php通过wp_remote_post. 最简单的解决方案是确保wp\\u remote\\u post拥有所有当前cookie,如下所示:

$nonce = wp_create_nonce( \'my-action\' );
foreach ( $_COOKIE as $name => $value ) {
    $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value );
}
$url = admin_url( \'admin-post.php\' );
$args = [
    \'method\'      => \'POST\',
    \'timeout\'     => 50,
    \'redirection\' => 5,
    \'blocking\'    => true,
    \'headers\'     => [] ,
    \'body\'        => [
        \'action\'   => \'gpc_nightly_job\',
        \'my_nonce\' => $nonce
    ],
    \'headers\'   => [
        \'cookie\' => implode( \'; \', $cookies ),
    ]
];
return wp_remote_post( $url, $args );
编辑:为了提高安全性,我在请求中添加了一个nonce,通过wp_verify_nonce.

相关推荐