douwuli4512 2011-12-19 08:33
浏览 29
已采纳

PHP代码无法在Facebook上运行

I'm using this code to merge text with an image and it's working fine. But when I use this code with Facebook app, it doesn't show any output. Can anyone please help me sorting out the problem? As far as I believe, the problem is with this line:

header("Content-Type: image/jpeg"); 

When I remove it, it shows some messed up output.

<?php 
header("Content-Type: image/jpeg"); 
$im = ImageCreateFrompng("jump_empty.png");  
$black = ImageColorAllocate($im, 255, 255, 255);
$start_x = 10; 
$start_y = 20; 
Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'ambient.ttf', "hellooo"); 
Imagejpeg($im, '', 100); 
ImageDestroy($im); 
?>

Here is the code I'm using for Facebook app:

<?php
include_once 'facebook.php';
include_once 'config.php';

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => '_____'
));
header("Content-Type: image/jpeg");
$session = $facebook->getSession();
if (!$session) {
    $url = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 0,
        'req_perms' => 'read_stream,publish_stream'
    ));
    echo "<script type='text/javascript'>top.location.href = '$url';</script>";
} else {
    try {
        $uid = $facebook->getUser();
        $me  = $facebook->api('/me');
        echo "Hey " . $me['name'] . "!<br />";
        $im      = ImageCreateFrompng("jump_empty.png");
        $black   = ImageColorAllocate($im, 255, 255, 255);
        $start_x = 10;
        $start_y = 20;
        Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'ambient.ttf', 'hello');
        Imagejpeg($im, '', 100);
        ImageDestroy($im);
    }
    catch (FacebookApiException $e) {
        echo "Error:" . print_r($e, true);
    }
}
?>
  • 写回答

1条回答 默认 最新

  • douqinlin5094 2011-12-19 08:53
    关注

    The problem is that you are trying to deliver two different content types in one page. You cannot send the image/jpeg header, telling the browser to expect an image, and then send js and or html. To accomplish this you should use two php scripts. One that sends the image/jpeg header and generates the image as you have in your first code block, and a second with normal headers that includes the first as the src of an image tag. If you want your image to be dynamic you could pass parameters to the image script as you would any php page.

    Your first script lets call it image.php would then be:

    <?php 
    
    header("Content-Type: image/jpeg"); 
    
    $im = ImageCreateFrompng("jump_empty.png");  
    
    $black = ImageColorAllocate($im, 255, 255, 255); 
    
    $start_x = 10; 
    $start_y = 20; 
    
    Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'ambient.ttf', "hellooo"); 
    
    Imagejpeg($im, '', 100); 
    
    ImageDestroy($im); 
    
    ?>
    

    Just as you had it. You second script would be:

    <?php
    
    include_once 'facebook.php';
    include_once 'config.php';
    
    $facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => '_____'
    ));
    
    $session = $facebook->getSession();
    
    if (!$session) {
    
    $url = $facebook->getLoginUrl(array(
            'canvas' => 1,
            'fbconnect' => 0,
            'req_perms' => 'read_stream,publish_stream',
    
    ));
    
    echo "<script type='text/javascript'>top.location.href = '$url';</script>";
    
    } else
    
     {
    
    try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
    echo "Hey " . $me['name'] . "!<br />";
    echo '<img src="image.php">';
    }
    catch (FacebookApiException $e) {
     echo "Error:" . print_r($e, true);
    }
    }
    ?>
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了