Wordpress rest\u pre\u serve\u请求生成PHP头警告

Wordpress rest\u pre\u serve\u请求生成PHP头警告,php,wordpress,rest,httprequest,httpserver,Php,Wordpress,Rest,Httprequest,Httpserver,我目前正在开发我的第一个Wordpress插件,我遇到了RESTAPI的一个问题,我真的不明白。也许有比我更好的方法,我还不知道,所以我很高兴能得到任何帮助 交易如下: 我正在创建一个API,它应该为Wordpress mysql数据库中的缓存数据提供服务。我正在将web资源中的数据(url、mime类型,[…])存储到我想提供服务的数据库中。要缓存的资源在策略文件中定义,该文件如下所示: [06-Aug-2020 10:47:03 UTC] PHP Warning: Cannot modif

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

交易如下:

我正在创建一个API,它应该为Wordpress mysql数据库中的缓存数据提供服务。我正在将web资源中的数据(url、mime类型,[…])存储到我想提供服务的数据库中。要缓存的资源在策略文件中定义,该文件如下所示:

[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
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如下所示:

[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
mypl/rest\u 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);
第41行和第92行标记在上述代码中

那么,我在这里做了什么根本错误的事情吗??或者,也许有人知道如何正确处理这些服务前的东西?


我感谢你的帮助

我找到了解决问题的方法

Wordpress在第596行的
../wp includes/rest-api.php中有一个默认的CORS过滤器,在这个过滤器中,Wordpress设置默认的头来发送“带api请求的跨源资源共享头”。()这个过滤器是用默认优先级(10)添加到RESTAPI的,因此我必须给我的自定义过滤器一个更高的优先级,以便在默认WP CORS过滤器之后执行它

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

警告消失了。

那么rest\u api.php的第92行和第41行是什么?到目前为止,你给我们看的内容甚至没有那么多行,我忘了加上了。我马上就去。第92行是“echo$vars['resource_data'];”第41行是rpc through端点中的curl_exec(…)。仍然没有足够的信息。假设您没有多次调用包含回音输出行的
SERVICE_static_resources
,我们还需要看看其他地方发生了什么(您模糊地提到了“online 596、597、598、599(其中的多个)”),我们目前无法知道您在做什么,按照什么顺序。据我所知,
service\u static\u resources
总是被称为bc,我通过add\u过滤器在每个
rest\u pre\u service\u请求上连接它。500多行中的错误在主
../wp includes/restapi.php
文件中。是的,我为您粘贴了它们。^它们是为个人的要求而设计的。我一直在做一些挖掘工作,问题出在wp-restapi默认值上(在第596行的wp-includes/rest-api.php中)。API尝试为所有请求设置cors头,看起来好像这些请求的添加是在我的自定义预服务请求之后进行的,因为两个过滤器(默认wp和我的自定义wp)都设置为优先级10。为了解决这个问题,我将过滤器的优先级设置为高于默认值10,这样调用肯定是在wp设置的默认cors头之后进行的,这解决了我的问题。