使用PHP标头输出缓冲

时间:2013-11-07 12:36:28

标签: php http-headers output-buffering

我正在尝试从WordPress安装中的数据创建一个vcard(WP部分我假设是无关紧要的)。我使用类似/inc/vcard.php?vcard=664的URL参数调用该文件,并且到目前为止具有以下代码:

<?php ob_start();

define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php' );

/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );
}

if (isset($_GET['vcard'])) {
    $attorney_id = $_GET['vcard'];

    $post = get_post($attorney_id);
    $slug = $post->post_name;

    if (!$post || get_post_type() !== 'attorneys') {
        echo 'Invalid attorney';
        return;
    }

    function field_check($field) {
        echo get_field($field) ? get_field($field) : '';
    }

    ?>

    BEGIN:VCARD
    VERSION:3.0
    REV:<?php echo get_the_date(); ?>
    FN:<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?> <?php field_check('last_name'); ?>
    N:<?php field_check('last_name'); ?>;<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?>

    TITLE:<?php field_check('position'); ?>

    ORG: Odin, Feldman &amp; Pittleman P.C.

    ADR;WORK:;;1775 Wiehle Avenue, Suite 400;Reston;Virginia;20190;United States of America

    URL:<?php field_check('blog_url'); ?>

    TEL;TYPE=WORK;VOICE:<?php field_check('phone_number'); ?>

    TEL;TYPE=WORK;Fax:<?php field_check('fax_number'); ?>

    EMAIL;TYPE=internet,pref:<?php field_check('email'); ?>

    URL;TYPE=WORK:<?php the_permalink(); ?>

    TZ:-0400
    END:VCARD

    <?php header('Content-Type: text/x-vcard; charset=utf-8');header('Content-Disposition: attachment; filename="' . $slug . '.vcf"'); exit();
}
else {
    echo 'Invalid input.';
}
$output = ob_get_contents();
ob_end_clean();
echo $output;

但似乎输出缓冲要么工作不正常,要么我还没有连接如何将两者(头和输出缓冲)连接在一起。任何帮助将不胜感激。谢谢!

快速说明一下,我在WordPress之外调用这个函数,这就是为什么我在输出缓冲区启动后“包含”WordPress的原因。

更新

在两个建议之后(将标题设置在缓冲区之外并将exit();移到底部),没有任何区别:

<?php ob_start();

define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php' );

/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );
}

if (isset($_GET['vcard'])) {
    $attorney_id = $_GET['vcard'];

    $post = get_post($attorney_id);
    $slug = $post->post_name;

    if (!$post || get_post_type() !== 'attorneys') {
        echo 'Invalid attorney';
        return;
    }

    function field_check($field) {
        echo get_field($field) ? get_field($field) : '';
    }

    ?>

    BEGIN:VCARD
    VERSION:3.0
    REV:<?php echo get_the_date(); ?>
    FN:<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?> <?php field_check('last_name'); ?>
    N:<?php field_check('last_name'); ?>;<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?>

    TITLE:<?php field_check('position'); ?>

    ORG: Odin, Feldman &amp; Pittleman P.C.

    ADR;WORK:;;1775 Wiehle Avenue, Suite 400;Reston;Virginia;20190;United States of America

    URL:<?php field_check('blog_url'); ?>

    TEL;TYPE=WORK;VOICE:<?php field_check('phone_number'); ?>

    TEL;TYPE=WORK;Fax:<?php field_check('fax_number'); ?>

    EMAIL;TYPE=internet,pref:<?php field_check('email'); ?>

    URL;TYPE=WORK:<?php the_permalink(); ?>

    TZ:-0400
    END:VCARD

    <?php
}
else {
    echo 'Invalid input.';
}
$output = ob_get_contents();
ob_end_clean();
header('Content-Type: text/x-vcard; charset=utf-8');header('Content-Disposition: attachment; filename="' . $slug . '.vcf"');
echo $output;
exit();

更新#2

我已经修改了更多的代码,以确保在任何地方都没有杂散的行:

<?php ob_start();

$slug = 'vcard';

define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php' );

/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );
}

if (isset($_GET['vcard'])) {
    $attorney_id = $_GET['vcard'];

    $post = get_post($attorney_id);
    $slug = $post->post_name;

    if (!$post || get_post_type() !== 'attorneys') {
        return 'Invalid attorney';
    }

    function field_check($field) {
        echo get_field($field) ? get_field($field) : '';
    }

?>
BEGIN:VCARD
VERSION:3.0
REV:<?php echo get_the_date(); ?>

FN:<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?> <?php field_check('last_name'); ?>

N:<?php field_check('last_name'); ?>;<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?>

TITLE:<?php field_check('position'); ?>

ORG: Odin, Feldman &amp; Pittleman P.C.
ADR;WORK:;;1775 Wiehle Avenue, Suite 400;Reston;Virginia;20190;United States of America
URL:<?php field_check('blog_url'); ?>

TEL;TYPE=WORK;VOICE:<?php field_check('phone_number'); ?>

TEL;TYPE=WORK;Fax:<?php field_check('fax_number'); ?>

EMAIL;TYPE=internet,pref:<?php field_check('email'); ?>

URL;TYPE=WORK:<?php the_permalink(); ?>

TZ:-0400
END:VCARD<?php
}
else {
    return 'Invalid input.';
}
$output = ob_get_contents();
header('Content-Type: text/x-vcard');
header('Content-Disposition: attachment; filename='.$slug.'.vcf');
return $output;
exit();

但仍然没有得到任何结果。我注意到没有“已发送标头”消息,但在尝试加载时没有“找不到网页”。如果我拿出标题调用,这是我得到的一些示例输入:

BEGIN:VCARD
VERSION:3.0
REV:November 4, 2013
FN:Sample Name
N:Adams;Robert C.
TITLE:Shareholder
ORG: Sample Company
ADR;WORK:;;Street;City;State;12345;United States of America
URL:
TEL;TYPE=WORK;VOICE:(555) 123-4567
TEL;TYPE=WORK;Fax:(555) 123-4567
EMAIL;TYPE=internet,pref:sample@test.com
URL;TYPE=WORK:http://www.google.com
TZ:-0400
END:VCARD

现在这是在源视图中,但在浏览器中它是全部在一行 - 在将标题呈现为vcard时是否重要?谢谢!

1 个答案:

答案 0 :(得分:0)

删除exit(),以便在发送输出之前代码不会停止。

相关问题