PHP 代码无法在 Facebook 上运行

发布于 2024-12-22 09:14:49 字数 1710 浏览 1 评论 0原文

我正在使用此代码将文本与图像合并,并且工作正常。但是当我在 Facebook 应用程序中使用此代码时,它不显示任何输出。谁能帮我解决这个问题吗?据我认为,问题出在这一行:

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

当我删除它时,它显示一些混乱的输出。

<?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); 
?>

这是我用于 Facebook 应用程序的代码:

<?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);
    }
}
?>

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);
    }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2024-12-29 09:14:49

问题是您试图在一个页面中提供两种不同的内容类型。您不能发送 image/jpeg 标头,告诉浏览器期待图像,然后发送 js 和/或 html。要实现此目的,您应该使用两个 php 脚本。一个发送 image/jpeg 标头并生成图像,就像您在第一个代码块中一样,第二个具有普通标头,其中包含第一个标头作为图像标记的 src。如果您希望图像是动态的,您可以像传递任何 php 页面一样将参数传递给图像脚本。

您的第一个脚本可以将其称为 image.php,然后是:

<?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); 

?>

就像您拥有的那样。你的第二个脚本是:

<?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);
}
}
?>

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文