【问题标题】:Post to Facebook page using php使用 php 发布到 Facebook 页面
【发布时间】:2012-04-08 12:20:27
【问题描述】:

我有一个要发布到 facebook 的脚本,但我在如何在 facebook 上发布数据时遇到了问题。

因为它可以帮助其他人这里是完整的代码

<?php
require 'facebook.php';
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 0;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = 0;

$pageId   = "XXXXXXXX";
$permSess = "XXXXXXXXXXXXXXXX";

$facebook = new Facebook(array(
    "appId" => "XXXXXXXXXX",
    "secret" => "XXXXXXXXXX",
    "cookie" => true
));

$page = $facebook->api("/{$pageId}");
?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    
    <head>
        <title>Post to Page Wall</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
        />
        <meta name="description" content="Post to page's wall" />
        <style>
            body {
                font-family:'Lucida Grande', Verdana, Arial, sans-serif;
                background-color: #f2f2f2;
            }
            h1 a {
                text-decoration: none;
                color: #3b5998;
            }
            h1 a:hover {
                text-decoration: underline;
            }
            form {
                border: 1px solid #eee;
                padding: 20px;
                width: 550px;
            }
            textarea, select, input, label {
                width: 500px;
                border: 1px solid #ddd;
                height: 20px;
                clear: both;
                margin: 10px;
            }
            textarea {
                height: 100px;
            }
            label {
                border: none;
                font-weight: bold;
            }
            input#submit {
                width: 100px;
            }
        </style>
    </head>
    
    <body>
        <h1>Post to Friend's Wall</h1>
        <?php if(isset($_POST[ 'submit'])) { $link=$
        _POST[ 'link']; $message=$ _POST[ 'message']; $attachment=a rray($message,$link
        ); $rest=$ facebook->api(array( "uid" => $pageId, "method" => "stream.publish", "access_token"
            => $permSess, "message" => $message )); } ?>
            <form id="Wall" name="Wall"
            method="post">
                <label for="URL">URL:</label>
                <input id="link" name="link">
                <label for="Message">Message:</label>
                <textarea id="message" name="message"></textarea>
                <input type="submit" name="submit" id="submit" value="Send!">
            </form>
    </body>

</html>

问题:

我使用的是发布here 的示例,并使用该示例在 URL 字段上提交的链接在 facebook 上发布,就像你自己做的一样(使用图像和网站描述)但使用我的代码我不能让它以同样的方式发生。

在“How can I post to the wall of a Facebook Fan Page using PHP and Open Graph API”上,一些用户发布了答案,但在我的情况下不起作用。

我的代码和其他网站上的代码的区别在于代码的“$attachment”部分

<?php
if (isset($_POST['submit'])) {
    $sendTo  = $_POST['friend'];
    $link    = $_POST['link'];
    $message = $_POST['message'];
    
    // all options: https://stackoverflow.com/questions/691425/how-do-you-post-to-the-wall-on-a-facebook-page-not-profile
    $attachment = array(
        'message' => $message,
        'link' => $link
    );
    
    if ($result = $facebook->api("/$sendTo/feed/", 'post', $attachment)) {
        $feedbackMessage = "Message sent to friend $sendTo";
    } else {
        $feedbackMessage = "Oops something went wrong";
    }
}
?>

所以...问题是如何正确地发布到 Facebook 页面而不损失任何好处或使用我的脚本格式化。

提前致谢

【问题讨论】:

  • 请在下面的答案中发布更改后的代码,而不是更改问题。

标签: php html facebook facebook-graph-api


【解决方案1】:

这会让你发布到 facebook 页面并保持格式,我明白你的意思了!

<?php 
if(isset($_POST['submit'])) {
            $link = $_POST['link'];
            $message = $_POST['message'];



$attachment = array(
        'access_token' => $permSess,
        'message' => $message,
        'link' => $link

    );
if(    $result = $facebook->api(
        "/".$pageId."/links",
        'post',
        $attachment
    )){
                $feedbackMessage = "Message sent to friend $sendTo";
            } else {
                $feedbackMessage = "Oops something went wrong";
            } 

}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-02
    • 2012-10-24
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多