使用 PHP 将事件发布到页面墙

发布于 2024-12-24 18:46:22 字数 5253 浏览 1 评论 0原文

我已经用头撞墙两周了,仔细研究网络,研究 stackoverflow,反复测试但未能找到一个脚本来将事件发布到粉丝页面。我终于得到了一个可以有效创建事件的脚本,但它没有出现在页面上。

在 DMCS 向我指出 PAGE 访问令牌后,我开始尝试生成它们。现在,问题是该事件出现在我的个人墙上,而不是我所定位的页面墙上。谁能看到我缺少什么吗?

这是最新编辑后的脚本:

    <?php
    $app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $my_url = "http://xxxxxxxxxxxxxxxxxxxxxx.com/testfiles/fbeventform.php";

//Going to get the PAGE access code
//First to get USER Access Code
   session_start();
   $code = $_REQUEST["code"];

   if(empty($code)) {
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'] . "&scope=create_event&scope=manage_pages";

     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }

   if($_REQUEST['state'] == $_SESSION['state']) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $access_token = @file_get_contents($token_url);
     $params = null;
     parse_str($access_token, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }

    echo '<hr />' . $access_token;

//Now, getting the PAGE Access token, using the user access token

    $page_token_url = "https://graph.facebook.com/" .  $page_id . "?fields=access_token&" . $access_token;
  $response = file_get_contents($page_token_url);

// Parse the return value and get the Page access token
  $resp_obj = json_decode($response,true);

  $page_access_token = $resp_obj['access_token'];

    echo '<hr />' . $page_access_token;

//Post the event--here's the form function

if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
    $msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
    $url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token;
    $params = array();
    // Prepare Event fields
    foreach($_POST as $key=>$value)
        if(strlen($value))
            $params[$key] = $value;

    // Check if we have an image
    if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
        $uploaddir = './upload/';
        $uploadfile = $uploaddir . basename($_FILES['picture']['name']);
        if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
            $params['picture'] = "@" . realpath($uploadfile);
        }
    }  

    // Start the Graph API call
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
    curl_close($ch);
    if(is_array($decoded) && isset($decoded['id'])) {
        // Event created successfully, now we can
        // a) save event id to DB AND/OR
        // b) show success message AND/OR
        // c) optionally, delete image from our server (if any)
        $msg = "Event created successfully: {$decoded['id']}";
    }
}
?>
    <form enctype="multipart/form-data" action="" method="post">
        <p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
        <p><label for="description">Event Description</label><textarea name="description"></textarea></p>
        <p><label for="location">Location</label><input type="text" name="location" value="" /></p>
        <p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
        <p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>
        <p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
        <p>
            <label for="privacy_type">Privacy</label>
            <input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open&nbsp;&nbsp;&nbsp;
            <input type="radio" name="privacy_type" value="CLOSED" />Closed&nbsp;&nbsp;&nbsp;
            <input type="radio" name="privacy_type" value="SECRET" />Secret&nbsp;&nbsp;&nbsp;
        </p>
        <p><input type="submit" value="Create Event" /></p>
    </form>
    </body>
    </html>

这有效地创建了事件,但它不会出现在我定位的页面墙上。我真的对此感到束手无策......

感谢任何和所有的帮助,我很乐意为未来的用户发布最终结果!

I've been knocking my head against a wall for two weeks now, poring over the net, poring over stackoverflow, and repeatedly testing and failing to get a script together to post an event to a Fan Page. I've finally gotten a script that effectively creates the event, but it doesn't appear on the page.

After DMCS pointed me along to the PAGE access tokens, I fiddled about to generate them. Now, the problem is that the event appears on my personal wall, not the page wall I'm targeting. Can anyone see what I'm missing?

Here's the script after the latest edits:

    <?php
    $app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $my_url = "http://xxxxxxxxxxxxxxxxxxxxxx.com/testfiles/fbeventform.php";

//Going to get the PAGE access code
//First to get USER Access Code
   session_start();
   $code = $_REQUEST["code"];

   if(empty($code)) {
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'] . "&scope=create_event&scope=manage_pages";

     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }

   if($_REQUEST['state'] == $_SESSION['state']) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $access_token = @file_get_contents($token_url);
     $params = null;
     parse_str($access_token, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }

    echo '<hr />' . $access_token;

//Now, getting the PAGE Access token, using the user access token

    $page_token_url = "https://graph.facebook.com/" .  $page_id . "?fields=access_token&" . $access_token;
  $response = file_get_contents($page_token_url);

// Parse the return value and get the Page access token
  $resp_obj = json_decode($response,true);

  $page_access_token = $resp_obj['access_token'];

    echo '<hr />' . $page_access_token;

//Post the event--here's the form function

if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
    $msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
    $url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token;
    $params = array();
    // Prepare Event fields
    foreach($_POST as $key=>$value)
        if(strlen($value))
            $params[$key] = $value;

    // Check if we have an image
    if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
        $uploaddir = './upload/';
        $uploadfile = $uploaddir . basename($_FILES['picture']['name']);
        if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
            $params['picture'] = "@" . realpath($uploadfile);
        }
    }  

    // Start the Graph API call
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
    curl_close($ch);
    if(is_array($decoded) && isset($decoded['id'])) {
        // Event created successfully, now we can
        // a) save event id to DB AND/OR
        // b) show success message AND/OR
        // c) optionally, delete image from our server (if any)
        $msg = "Event created successfully: {$decoded['id']}";
    }
}
?>
    <form enctype="multipart/form-data" action="" method="post">
        <p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
        <p><label for="description">Event Description</label><textarea name="description"></textarea></p>
        <p><label for="location">Location</label><input type="text" name="location" value="" /></p>
        <p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
        <p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>
        <p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
        <p>
            <label for="privacy_type">Privacy</label>
            <input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open   
            <input type="radio" name="privacy_type" value="CLOSED" />Closed   
            <input type="radio" name="privacy_type" value="SECRET" />Secret   
        </p>
        <p><input type="submit" value="Create Event" /></p>
    </form>
    </body>
    </html>

This effectively creates the event, but it doesn't appear on the page wall I'm targetting. I'm really at the end of my rope over this...

Any and all help is appreciated, and I will be happy to post final results for future users!

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

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

发布评论

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

评论(1

陪你搞怪i 2024-12-31 18:46:22

调试您正在使用的访问令牌 https://developers.facebook.com/tools/lint 并确保您拥有 PAGE 访问令牌而不是 USER 访问令牌。

要获取页面访问令牌,请参阅 https://developers.facebook.com/ 的“页面登录”部分文档/身份验证/

Debug the access token you're using at https://developers.facebook.com/tools/lint and ensure you have a PAGE access token and not a USER access token.

For getting a page access token, see "Page Login" part of https://developers.facebook.com/docs/authentication/

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