使用 FORM

发布于 2024-12-08 16:10:00 字数 1164 浏览 0 评论 0原文

我是尝试创建 Facebook 应用程序的初学者。代码如下:

<?php
 $app_id = "xxx";
 $canvas_page = "https://apps.facebook.com/xxx/";

 $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=user_birthday";

 $signed_request = $_REQUEST["signed_request"];
 list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
 $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

 if (empty($data["user_id"])) {
        echo("<script> top.location.href='" . $auth_url . "'</script>");
 } else {
        echo ("<p>Welcome. Your Facebook ID is " . $data["user_id"] . "</p>");

        if (isset($_POST['salary'])) {
            echo "<p>You wrote salary: " . $_POST['salary'] . "</p>";
        }
        else {
            echo "<p>You didn't wrote salary.</p>";
        };

        echo "<form method='post' action='$canvas_page'>";
        echo "<input type='text' name='salary'>";
        echo "<input type='submit'>";
        echo "</form>";
 }
?>

为什么结果总是显示“你没有写工资”。

I'm beginner who try to create Facebook App. Here is the code:

<?php
 $app_id = "xxx";
 $canvas_page = "https://apps.facebook.com/xxx/";

 $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=user_birthday";

 $signed_request = $_REQUEST["signed_request"];
 list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
 $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

 if (empty($data["user_id"])) {
        echo("<script> top.location.href='" . $auth_url . "'</script>");
 } else {
        echo ("<p>Welcome. Your Facebook ID is " . $data["user_id"] . "</p>");

        if (isset($_POST['salary'])) {
            echo "<p>You wrote salary: " . $_POST['salary'] . "</p>";
        }
        else {
            echo "<p>You didn't wrote salary.</p>";
        };

        echo "<form method='post' action='$canvas_page'>";
        echo "<input type='text' name='salary'>";
        echo "<input type='submit'>";
        echo "</form>";
 }
?>

Why the result always show "You didn't wrote salary".

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

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

发布评论

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

评论(1

无人问我粥可暖 2024-12-15 16:10:00

该代码确实可以在我的画布上运行,因此问题肯定出在 auth url 上。简要地:
如果这个条件

if (empty($data["user_id"]))

成立,您将看到用户被重定向到 auth url,然后返回到您的画布页面。

您可以检查 url 是否发生这种情况:如果 url 末尾附加了 code=something ,则意味着用户已被重定向,这样做显然会丢失 while _POST 内容。

您可以以防万一有 $data 的 print_r 或 var_dump 以确保它实际上填充了某些内容。

再见=)

编辑14.10.11:实际上你的代码是正确的,没有任何问题,所以示例代码是你自己的代码

That code does actually work on my canvas, so the problem must be with the auth url. Briefly:
if this condition:

if (empty($data["user_id"]))

gets to be true, what you will have will be the user redirected to the auth url and after that back to your canvas page.

You can check if this happens looking at the url: if there is a code=something attached to the end of the url it means that the user has been redirected, doing this obviously you lose the while _POST content.

You could just in case have a print_r or var_dump of $data to be sure that it is actually filled with something.

Bye =)

EDIT 14.10.11: Actually your code is right, there's nothing wrong in it, so the sample code is your own code

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