Php 发送头插件中echo的替代方案

Php 发送头插件中echo的替代方案,php,wordpress,echo,Php,Wordpress,Echo,我从Wordpress获得以下PHP错误: Warning: Cannot modify header information - headers already sent by (output started at domain/public_html/sdapi/wp-content/plugins/testpost/testpost.php:74) in domain/public_html/sdapi/wp-admin/post.php on line 235 Warning: Can

我从Wordpress获得以下PHP错误:

Warning: Cannot modify header information - headers already sent by (output started at domain/public_html/sdapi/wp-content/plugins/testpost/testpost.php:74) in domain/public_html/sdapi/wp-admin/post.php on line 235

Warning: Cannot modify header information - headers already sent by (output started at domain/public_html/sdapi/wp-content/plugins/testpost/testpost.php:74) in domain/public_html/sdapi/wp-includes/pluggable.php on line 1196
使用此代码:

    <?php
/**
 * Plugin Name: testpost
 */
add_action( 'publish_post', 'testpost', 10, 1 );
function testpost( $post ) {

// ***** Get variables *****
$post_id = $post;
$post_object = get_post( $post_id );
$post_object->post_content;

$title = $post_object->post_title;
$source_url = get_permalink( $post_id );
$body = $post_object->post_content;

$titleprint = preg_replace("/\r?\n/", "\\n", addslashes($title));    
$bodyprint = preg_replace("/\r?\n/", "\\n", addslashes($body));
$posturlprint = preg_replace("/\r?\n/", "\\n", addslashes($source_url));




// ***** Run script *****
echo "<script>

var Request = new XMLHttpRequest();

Request.open('POST', '****POSTURL****');

Request.setRequestHeader('Content-Type', 'application/json');
Request.setRequestHeader('Accept', 'application/json');
Request.setRequestHeader('Authorization', '****TOKEN****');

Request.onreadystatechange = function () {
if (this.readyState === 4) {
}
};

var body = {
'article': {
'uuid': '090bda74-b021-4c7c-a44a-44f33bba32142',
'title': '". $titleprint ."',
'source_url': '". $posturlprint ."',
'body': '". $bodyprint ."'
}
};

Request.send(JSON.stringify(body));


</script>";




}
?>


*
之前的空白似乎存在于代码的开头。这就是为什么你会收到这个警告。php标记的开头和结尾不应该有空格。

尝试使用
ob_start()以防止标题已发送问题。这个问题主要是由于php代码中有多余的空格,或者在关闭
?>
标记之前您有太多的空格,您也可以避免使用这个关闭标记,并请删除这些空格

<?php
 ob_start();
/**
 * Plugin Name: testpost
 */
add_action( 'publish_post', 'testpost', 10, 1 );
function testpost( $post ) {

// ***** Get variables *****
$post_id = $post;
$post_object = get_post( $post_id );
$post_object->post_content;

$title = $post_object->post_title;
$source_url = get_permalink( $post_id );
$body = $post_object->post_content;

$titleprint = preg_replace("/\r?\n/", "\\n", addslashes($title));    
$bodyprint = preg_replace("/\r?\n/", "\\n", addslashes($body));
$posturlprint = preg_replace("/\r?\n/", "\\n", addslashes($source_url));

// ***** Run script *****
echo "<script>

var Request = new XMLHttpRequest();

Request.open('POST', '****POSTURL****');

Request.setRequestHeader('Content-Type', 'application/json');
Request.setRequestHeader('Accept', 'application/json');
Request.setRequestHeader('Authorization', '****TOKEN****');

Request.onreadystatechange = function () {
if (this.readyState === 4) {
}
};

var body = {
'article': {
'uuid': '090bda74-b021-4c7c-a44a-44f33bba32142',
'title': '". $titleprint ."',
'source_url': '". $posturlprint ."',
'body': '". $bodyprint ."'
}
};

Request.send(JSON.stringify(body));

</script>";
}
?>


尝试使用ob_start();在php脚本的开头,但是第一行有空格…在
之前,错误似乎是指第74行…但是没有第74行。这就是你的全部插件文件吗?我认为你应该使用WordPress API函数来代替
echo()
,但我不知道。你最好继续问。谢谢你的回复!-Sachin:ob_start()阻止其余代码运行(见下文)-rnevius:空白确实是通过将代码粘贴到此处创建的,我将把它添加到问题中。这确实是整个插件文件。我没有看到提到第74行的错误。我确实有第74行,但就在JS的中间…谢谢ARSH的建议。这确实消除了错误,但不幸的是阻止了其余代码的运行,ob_start()应该这样做吗?没有ob_start();不会停止您的代码运行。很遗憾,在添加ob_start()时,我的脚本没有运行。据我所知,这是因为ob_start()忽略了我在脚本中进行的重定向。我决定走另一条路;我已经在cms中添加了一个发送帖子的按钮,现在标题没有混淆。感谢您的帮助:)由于粘贴在此处,因此添加了空格。档案里没有,我应该提到的!