I am currently developing my first Wordpress plugin and I've encountered an issue with the REST API which I don't really understand. Maybe there is a better approach than mine, that I am not yet aware of, so I am happy for any help that's out there!
Here's the deal:
I am creating an API that is supposed to serve cached data from the Wordpress mysql db. I am storing data (url, mime-type, [...]) from web resources to the db which I then want to serve. The resources to be cached are defined in a policy file which looks something like this:
policy.json
{
"files":
[
{
"resource_url": "https://example.come/assets/images/image.png",
(...)
},
(...)
]
}
Since the number of files is dynamic my service needs to adjust to whatever custom routes the policy file dictates. E.g. In the above case it is supposed to serve the resource via my Wordpress site, which initially came from example.com. So if
https://my-wordpress-site.com/wp-json/example/v1/assets/images/image.png is requested, the cached file from https://example.come/assets/images/image.png is supposed to be served.
My REST API looks something like this:
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);
The way I thought about it was, I create a READABLE (GET) rest route, which accepts all requests on the rest API. Once the request comes in it is being intercepted with the rest_pre_serve_request filter, in order to check the database whether or not the requested file is cached and if so serve it. Here's my approach on that:
According to the documentation I can simply echo the data from the filter and return true or false to indicate whether the request has already been served or not. In my case though, I also have to add additional data such as the Status-Code and the Content-Type for the response, in order for the clients to be able to process it correctly. Now here's the Crux. The logic works exactly like I want it to and it serves the requests like they're supposed to be, but still I get the classic headers already sent warnings from PHP, which look like this:
[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
Lines 41 and 92 are marked in the code above.
So, is there anything that I am doing fundamentally wrong here?? Or maybe someone knows how to handle that pre serving stuff properly?
I appreciate your help!!
I found a solution to my issue.
Wordpress has a default CORS filter in .../wp-includes/rest-api.php on line 596, in this filter Wordpress sets default headers to send "Cross-Origin Resource Sharing headers with API requests". (https://github.com/WordPress/wordpress-develop/blob/5.4/src/wp-includes/rest-api.php#L596) This filter is added with default priority (10) to the REST API therefore I just had to give my custom filter a higher priority in oder to have it executed after the default WP CORS filter.
add_filter('rest_pre_serve_request', 'serve_static_resources', 11, 4);
And the warnings disappeared.
I have a PHP file and whenever running a file in PHP phpdesktop-chrome (APP) I am getting below and I have only the following code in my file.
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
Following is my error code.
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\abc\www\backend\check.php:2)
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\abc\www\backend\check.php:3)
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\abc\www\backend\check.php:4)
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\abc\www\backend\check.php:5)
Try to insert ob_start(); after the first <?php opening tag.
Sessions does not work when I change directory. I mean my files that contain codes for sessions run on the main directory. However, when I upload same files to different directory, they does not run. The same codes, files but not working in different directory. What is the problem? I confused.
Here is my codes:
<?php
ob_start();
session_start();
include("db.php");
$rota=strip_tags($_POST["rota"]);
$date=strip_tags($_POST["date"]);
$_SESSION['rota']=$rota;
$_SESSION['date']=$date;
?>
------
<?php session_start();
ob_start();
include("db.php");
$date=$_SESSION["date"];
$rota=$_SESSION["rota"];
echo "Date : <b>".$date."<br><br></b>";
?>
And here is my error log:
[12-Aug-2016 13:51:41 UTC] PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/karelyac/public_html/reserve/en/y1s3.php:1) in /home/karelyac/public_html/reserve/en/y1s3.php on line 3
[12-Aug-2016 13:51:41 UTC] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/karelyac/public_html/reserve/en/y1s3.php:1) in /home/karelyac/public_html/reserve/en/y1s3.php on line 3
[12-Aug-2016 13:51:41 UTC] PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/karelyac/public_html/reserve/en/db.php on line 7
[12-Aug-2016 13:51:43 UTC] PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/karelyac/public_html/reserve/en/y1s2.php:1) in /home/karelyac/public_html/reserve/en/y1s2.php on line 3
[12-Aug-2016 13:51:43 UTC] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/karelyac/public_html/reserve/en/y1s2.php:1) in /home/karelyac/public_html/reserve/en/y1s2.php on line 3
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1024: Cannot modify header information - headers already sent by (output started at /application/libraries/phpbb_library.php:1)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1024: Cannot modify header information - headers already sent by (output started at /application/libraries/phpbb_library.php:1)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1024: Cannot modify header information - headers already sent by (output started at /application/libraries/phpbb_library.php:1)
How to fix this ?
I red in the phpbb3 library author theme, one of the posts has the same issue, and he fixed it by ‘not cloasing the controller file’, but what did he mean realy ? Sme help ?
It means that he removed ?> in the end of some php file.
But in your case all you need is just to cut off the BOM from the /application/libraries/phpbb_library.php file
I'm trying to setup a tiny sandbox on a local machine to play around with Drupal. I created a few CCK types; in order to create a few nodes I wrote the following script:
chdir('C:\..\drupal');
require_once '.\includes\bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => 'my_type');
$link = mysql_connect(..);
mysql_select_db('my_db');
$query_bldg = '
SELECT stuff
FROM table
LIMIT 10
';
$result = mysql_query($query_bldg);
while ($row = mysql_fetch_object($result)) {
$form_state = array();
$form_state['values']['name'] = 'admin';
$form_state['values']['status'] = 1;
$form_state['values']['op'] = t('Save');
$form_state['values']['title'] = $row->val_a;
$form_state['values']['my_field'][0]['value'] = $row->val_b;
## About another dozen or so of similar assignments...
drupal_execute('node_form', $form_state, (object)$node);
}
Here are a few relevant lines from php_errors.log:
[12-Jun-2010 05:02:47] PHP Notice: Undefined index: REMOTE_ADDR in C:\..\drupal\includes\bootstrap.inc on line 1299
[12-Jun-2010 05:02:47] PHP Notice: Undefined index: REMOTE_ADDR in C:\..\drupal\includes\bootstrap.inc on line 1299
[12-Jun-2010 05:02:47] PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\..\drupal\includes\bootstrap.inc:1299) in C:\..\drupal\includes\bootstrap.inc on line 1143
[12-Jun-2010 05:02:47] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\..\drupal\includes\bootstrap.inc:1299) in C:\..\drupal\includes\bootstrap.inc on line 1143
[12-Jun-2010 05:02:47] PHP Warning: Cannot modify header information - headers already sent by (output started at C:\..\drupal\includes\bootstrap.inc:1299) in C:\..\drupal\includes\bootstrap.inc on line 709
[12-Jun-2010 05:02:47] PHP Warning: Cannot modify header information - headers already sent by (output started at C:\..\drupal\includes\bootstrap.inc:1299) in C:\..\drupal\includes\bootstrap.inc on line 710
[12-Jun-2010 05:02:47] PHP Warning: Cannot modify header information - headers already sent by (output started at C:\..\drupal\includes\bootstrap.inc:1299) in C:\..\drupal\includes\bootstrap.inc on line 711
[12-Jun-2010 05:02:47] PHP Warning: Cannot modify header information - headers already sent by (output started at C:\..\drupal\includes\bootstrap.inc:1299) in C:\..\drupal\includes\bootstrap.inc on line 712
[12-Jun-2010 05:02:47] PHP Notice: Undefined index: REMOTE_ADDR in C:\..\drupal\includes\bootstrap.inc on line 1299
[12-Jun-2010 05:02:48] PHP Fatal error: Allowed memory size of 239075328 bytes exhau sted (tried to allocate 261904 bytes) in C:\..\drupal\includes\form.inc on line 488
[12-Jun-2010 05:03:22] PHP Fatal error: Allowed memory size of 239075328 bytes exhausted (tried to allocate 261904 bytes) in C:\..\drupal\includes\form.inc on line 488
[12-Jun-2010 05:04:34] PHP Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 261904 bytes) in Unknown on line 0
At this point any action php takes results in the last error shown above. I tried increasing the value of memory_limit in php.ini before the final Fatal error which obviously didn't help.
How can the error be eliminated? Am I on a correct path to migrating data into Drupal or should the cck tables be operated on directly?
Windows XP
PHP 5.3.2 VC6
Apache 2.2
If you're trying to migrate data from sql (or anywhere else), you'll find doing your own direct import is fairly complicated. Have a look at this method:
http://www.lullabot.com/articles/drupal-data-imports-migrate-and-table-wizard
WHat exactly is that 'node_form' call doing? I'm guessing it's that $form_state data and building some huge in-memory structure from it. That means:
a) You have to increase (or eliminate) the memory limit - Drupal's building something big, and you're not giving it the headroom to work
b) Change the node_form stuff so it builds smaller chunks at a time. Maybe flush output to the client at intervals? Save to disk?