【问题标题】:Wordpress rest_pre_serve_request produces PHP header warningsWordpress rest_pre_serve_request 产生 PHP 标头警告
【发布时间】:2020-08-06 11:32:52
【问题描述】:

我目前正在开发我的第一个 Wordpress 插件,但我遇到了一个我不太了解的 REST API 问题。也许有比我更好的方法,我还不知道,所以我很高兴能得到任何帮助!

这是交易:

我正在创建一个 API,它应该提供来自 Wordpress mysql 数据库的缓存数据。我将数据(url、mime-type、[...])从网络资源存储到我想要服务的数据库中。要缓存的资源在策略文件中定义,如下所示:

policy.json

{
    "files":
    [
        {
            "resource_url": "https://example.come/assets/images/image.png",
            (...)
        },
        (...)
    ]
}

由于文件数量是动态的,我的服务需要根据策略文件规定的任何自定义路由进行调整。例如。在上述情况下,它应该通过我的 Wordpress 站点提供资源,该站点最初来自 example.com。因此,如果 https://my-wordpress-site.com/wp-json/example/v1/assets/images/image.png 被请求,来自https://example.come/assets/images/image.png 的缓存文件应该被提供。

我的 REST API 如下所示:

mypl/rest_api.php

<?php

function custom_resource_endpoint() {
    return new WP_REST_Response("The requested resource could not be found.", 400);    
}


function rpc_through_endpoint( $request_data ) {

    $route = $request_data->get_route();
    $header = $request_data->get_headers();
    $data = $request_data->get_body();

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://example.com" . str_replace('example/v1/', '', $route));
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/octet-stream'));
            
41  $response = curl_exec($curl);
    curl_close($curl);
   
    return new WP_REST_Response($response, 200);
}


function register_custom_routes() {
    $rest_prefix = 'example/v1';

    register_rest_route($rest_prefix, '/rpc', array(
        'methods' => WP_REST_Server::CREATABLE,
        'callback' => 'rpc_through_endpoint'
    ));

    register_rest_route($rest_prefix, '(/w)', array(
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'custom_resource_endpoint'
    ));    
}

add_action('rest_api_init', 'register_custom_routes');


// Interceptor.

function serve_static_resources($served, $result, $request, $server) {

    $route = $request->get_route();

    if (ends_with($route, 'rpc')) {
        $served = false;
    } else {

        $file_uri = str_replace('/example/v1/', 'https://example/', $route);
        
        if ($file_uri != null) {
            $resource = checkLatestResourceByUri($file_uri);
            if(count($resource) > 0) {
                $vars=get_object_vars($resource[0]);
                http_response_code(200);
                header( 'Content-Type: ' . $vars['resource_mime_type'], true );
92              echo $vars['resource_data'];
                $served = true;
            }
        }
    }

    return $served;
}

add_filter('rest_pre_serve_request', 'serve_static_resources', 10, 4);

我的想法是,我创建了一个 READABLE (GET) rest 路由,它接受 rest API 上的所有请求。一旦请求进入,它就会被rest_pre_serve_request 过滤器拦截,以检查数据库是否缓存了请求的文件,如果有,则提供服务。这是我的方法:

根据文档,我可以简单地回显过滤器中的数据并返回 truefalse 以指示请求是否已被服务。不过,就我而言,我还必须为响应添加额外的数据,例如 Status-CodeContent-Type,以便客户端能够正确处理它。 现在是关键。 逻辑完全按照我的意愿工作,它按照应有的方式处理请求,但我仍然得到经典的标头已发送警告 来自 PHP,如下所示:

[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 599
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 599
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 599

[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:41) in /var/www/html/wp-includes/rest-api/class-wp-rest-server.php on line 1337
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:41) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:41) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:41) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:41) in /var/www/html/wp-includes/rest-api.php on line 599

[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 596
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 597
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 598
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 599
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 599
[06-Aug-2020 10:47:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/mypl/rest_api.php:92) in /var/www/html/wp-includes/rest-api.php on line 599

第 41 行和第 92 行标记在上面的代码中。

那么,我在这里做错了什么吗?或者也许有人知道如何正确处理这些预服务?

感谢您的帮助!

【问题讨论】:

  • 那么你的rest_api.php 的第92 和41 行的 是什么?到目前为止,您向我们展示的内容甚至不包含那么多行。
  • 我忘了补充。我会马上做的。第 92 行是“echo $vars['resource_data'];”第 41 行是 rpc 中的 curl_exec(...) 通过端点。
  • 仍然没有足够的信息。假设您没有多次调用包含该回显输出行的serve_static_resources,我们还需要查看其他地方发生的情况(您在第 596 行用 “模糊地提到了这一点, 597, 598, 599(多个)”),我们目前无法确定您具体在哪里以及按什么顺序进行操作。
  • 好吧,据我了解,serve_static_resources 总是被称为 bc 我通过每个 rest_pre_serve_request 上的 add_filter 连接它。 500 多行中的错误位于主要的 .../wp-includes/rest-api.php 文件中。
  • 是的,我为你粘贴了它们。 ^^ 它们是针对个人要求的。我一直在进行一些挖掘,wp-rest api 默认值是问题所在(在 wp-includes/rest-api.php 的第 596 行)。 API 尝试为所有请求设置 cors 标头,并且看起来好像在我的自定义保留请求之后添加了这些标头,因为两个过滤器(默认 wp 和我的自定义一个)都设置为优先级 10。解决问题,我已将过滤器设置为高于默认 10 的优先级,因此调用肯定在 wp 设置的默认 cors 标头之后,这解决了我的问题。

标签: php wordpress rest httprequest httpserver


【解决方案1】:

我找到了解决问题的方法。

Wordpress 在.../wp-includes/rest-api.php on line 596 中有一个默认的 CORS 过滤器,在此过滤器中,Wordpress 设置默认标头以发送“带有 API 请求的跨源资源共享标头”。 (https://github.com/WordPress/wordpress-develop/blob/5.4/src/wp-includes/rest-api.php#L596) 这个过滤器以默认优先级 (10) 添加到 REST API,因此我只需要给我的自定义过滤器一个更高的优先级,以便在默认的 WP CORS 过滤器之后执行它。

add_filter('rest_pre_serve_request', 'serve_static_resources', 11, 4);

警告消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多