Facebook PHP SDK和Wordpress错误
我使用WAMP、Wordpress和PHPEdit IDE设置了开发人员环境。我在侧边栏中使用Facebook、Twitter和YouTube API。我使用Facebook的PHPSDK来显示信息(没有登录或管理功能)。由于FB SDK和WP使用session_start(),我得到以下警告: 警告:会话启动()[函数.会话启动]:无法发送会话 缓存限制器-标头已发送(输出开始于 C:\wamp\www\dfi\wp content\themes\dfi\header.php:12)中 C:\wamp\www\dfi\wp content\themes\dfi\api\facebook.php,第36行 我试图通过使用警告输出来解决这个问题,但考虑以下几点并没有帮助。我知道如何在Facebook PHP SDK和Wordpress错误,php,facebook,wordpress,session,sdk,Php,Facebook,Wordpress,Session,Sdk,我使用WAMP、Wordpress和PHPEdit IDE设置了开发人员环境。我在侧边栏中使用Facebook、Twitter和YouTube API。我使用Facebook的PHPSDK来显示信息(没有登录或管理功能)。由于FB SDK和WP使用session_start(),我得到以下警告: 警告:会话启动()[函数.会话启动]:无法发送会话 缓存限制器-标头已发送(输出开始于 C:\wamp\www\dfi\wp content\themes\dfi\header.php:12)中 C:\
前后清除空格和字符,并将会话\u start()放在任何http输出之前。我使用unix line enders和UTF8编码,没有BOM表。我的主机服务器未设置输出缓冲
header.php第11到13行
11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
12 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes();?>>
13 <head>
我认为我无法阻止FB或WP调用session_start(),而不破坏一切。我如何让Wordpress和Facebook在没有这个错误的情况下很好地配合使用
编辑:
要停止显示警告,我将@放在会话_start()前面
这只是一个解决方法,我仍然希望找到问题的根源。正如您在评论中所发现的,问题不是包含PHP文件,而是定义类的地方。创建Facebook类的实例可以在
wp
hook中安全地完成(据我所知,它对我很有用)。这将允许您在任何HTML输出之前定义类的实例,然后您可以在类中的任何位置使用该变量
但是,您确实希望确保只包含该类一次,但是可以根据需要多次实例化该类
下面是一个基本示例,可以帮助您开始:
if( !class_exists( 'Facebook' ) ) {
require_once 'facebook.php';
}
if( !class_exists( 'YourClass' ) ) {
class YourClass {
public $facebook = null;
public function __construct() {
add_action( 'wp', array( $this, 'define_facebook' ) );
add_action( 'any_hook_after_wp', array( $this, 'example_usage' ) );
}
public function define_facebook() {
global $post;
// Assuming you are using post meta for the app ID and secret, you can use other methods though
$app_id = get_post_meta( $post->ID, 'appId', true );
$app_secret = get_post_meta( $post->ID, 'appSecret', true );
$this->facebook = new Facebook( array( 'appId' => $app_id, 'secret' => $app_secret ) );
}
public function example_usage() {
if( !is_null( $this->facebook ) ) {
// Lets see what we have here..
echo "<pre>";
print_r( $this->facebook );
echo "</pre>";
exit;
}
}
}
}
如果(!class_存在('Facebook')){
需要一次“facebook.php”;
}
如果(!class_存在('YourClass')){
你的班级{
public$facebook=null;
公共函数构造(){
添加动作('wp',数组('this',define_facebook');
添加动作('any_hook'u after_wp',数组($this,'example_usage');
}
公共函数define_facebook(){
全球$员额;
//假设你使用post meta作为应用ID和secret,你也可以使用其他方法
$app\u id=get\u post\u meta($post->id,'appId',true);
$app\u secret=get\u post\u meta($post->ID,'appSecret',true);
$this->facebook=newfacebook(数组('appId'=>$app\u id,'secret'=>$app\u secret));
}
公共函数示例_用法(){
如果(!为空($this->facebook)){
//让我们看看这里有什么。。
回声“;
// add it into functions.php in theme folder
add_action('init', 'themename_wp_session_start');
function themename_wp_session_start(){
if( !session_id() ){
session_start();
}
}
打印($this->facebook);
回声“;
// add it into functions.php in theme folder
add_action('init', 'themename_wp_session_start');
function themename_wp_session_start(){
if( !session_id() ){
session_start();
}
}
出口
}
}
}
}
正如您在评论中发现的,问题不在于包含PHP文件,而在于定义类的位置。创建Facebook类的实例可以在wp
hook中安全地完成(据我所知,它对我很有用)。这将允许您在任何HTML输出之前定义类的实例,然后您可以在类中的任何位置使用该变量
但是,您确实希望确保只包含该类一次,但是可以根据需要多次实例化该类
下面是一个基本示例,可以帮助您开始:
if( !class_exists( 'Facebook' ) ) {
require_once 'facebook.php';
}
if( !class_exists( 'YourClass' ) ) {
class YourClass {
public $facebook = null;
public function __construct() {
add_action( 'wp', array( $this, 'define_facebook' ) );
add_action( 'any_hook_after_wp', array( $this, 'example_usage' ) );
}
public function define_facebook() {
global $post;
// Assuming you are using post meta for the app ID and secret, you can use other methods though
$app_id = get_post_meta( $post->ID, 'appId', true );
$app_secret = get_post_meta( $post->ID, 'appSecret', true );
$this->facebook = new Facebook( array( 'appId' => $app_id, 'secret' => $app_secret ) );
}
public function example_usage() {
if( !is_null( $this->facebook ) ) {
// Lets see what we have here..
echo "<pre>";
print_r( $this->facebook );
echo "</pre>";
exit;
}
}
}
}
如果(!class_存在('Facebook')){
需要一次“facebook.php”;
}
如果(!class_存在('YourClass')){
你的班级{
public$facebook=null;
公共函数构造(){
添加动作('wp',数组('this',define_facebook');
添加动作('any_hook'u after_wp',数组($this,'example_usage');
}
公共函数define_facebook(){
全球$员额;
//假设你使用post meta作为应用ID和secret,你也可以使用其他方法
$app\u id=get\u post\u meta($post->id,'appId',true);
$app\u secret=get\u post\u meta($post->ID,'appSecret',true);
$this->facebook=newfacebook(数组('appId'=>$app\u id,'secret'=>$app\u secret));
}
公共函数示例_用法(){
如果(!为空($this->facebook)){
//让我们看看这里有什么。。
回声“;
// add it into functions.php in theme folder
add_action('init', 'themename_wp_session_start');
function themename_wp_session_start(){
if( !session_id() ){
session_start();
}
}
打印($this->facebook);
回声“;
// add it into functions.php in theme folder
add_action('init', 'themename_wp_session_start');
function themename_wp_session_start(){
if( !session_id() ){
session_start();
}
}
出口
}
}
}
}
您可以使用Hook Action init检查会话id是否存在
您可以使用Hook Action init检查会话标识是否存在
你在哪里包括了PHP Facebook实例。我试过一次require_,并在functions.PHP顶部和header.PHP的第一行找到了_模板(WP函数),但没有成功。我认为这是因为wordpress在引用这些页面之前调用了session\u start。正确,看看这是否为您指明了正确的方向。问题提到了相同的session\u start()警告,但没有解决它。在最后一个例子中,他调用
require'facebook.php'然后只创建facebook类的一个实例,其中php短代码在某个随机位置调用。如果我知道他在哪里打电话,可能会让我有更好的了解。在帖子上留下评论,让他注意到这个问题。你在哪里包括PHP Facebook实例。我试过一次require_,并在functions.PHP顶部和header.PHP的第一行找到了_模板(WP function),但运气不好。我认为这是因为wordpress在引用这些页面之前调用了session\u start。正确,看看这是否为您指明了正确的方向。问题提到了相同的session\u start()警告,但没有解决它。在最后一个例子中,他调用require'facebook.php'代码>然后只有c