php - 从 url/超链接发布 Open Graph Story

标签 php facebook facebook-graph-api facebook-opengraph

我已经在我的 Facebook 应用程序的开放图谱模块中成功创建了一个故事。

故事是:

“John [选择] [巴西] 作为 MYDOMAIN 足球世界杯的新世界冠军”

[pick] 是 Action ,[country] 是用户选择的国家。这似乎工作正常。但是下一步让我感到困惑。

我希望在我的网站上通过 Facebook 登录的访问者单击他们认为将赢得即将到来的世界杯的国家/地区的国旗。随后,上述故事被发布到他们的时间线上。

我不知道如何设置它。 Facebook 的开发中心很广泛,但对于像我这样的 PHP 新手来说,这一步过程不是很清楚。

假设我页面上的标志所代表的超链接需要看起来像这样:

http://mydomain.com/storypublisher.php?&country= "巴西"&image="巴西国旗.jpg"等

所以我的问题是:

  • storypublisher.php 的正确代码是什么?
  • 我上面提到的 url 的正确格式是什么?

我已经阅读了有关“食谱”等的各种教程,但当涉及到您实际开始发布上述故事的部分时,它们让我感到困惑。

最佳答案

像这样?根据您对代码部分的需要采用它。使用 Facebook PHP SDK .

// 0. We don't need to clear session
require_once realpath(dirname(__FILE__)) . 'facebook.php';

$facebook = new Facebook(array(
    'appId' => '',
    'secret' => '',
));

$fbCurrentUserID = $facebook->getUser();

// 1. If we cant get the user log him in and request permissions This requests combined permissions (basic + post)
if (!$fbCurrentUserID){
    $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'publish_actions',            
        'redirect_uri' => $current_url, // Replace here
    ));
    $this->abortToUrl($loginUrl);
}

// 2. So we do have a current FB user. Lets try to read data (Maybe he declined perms)
try {
    // 2.a Check if has granted us the perms and post
    $isGranted = $facebook->api(array(
        "method"    => "users.hasAppPermission",
        "ext_perm"  => "publish_actions",
        "uid"       => $fbCurrentUserID,
    ));

    if (!$isGranted){
        $login_url = $facebook->getLoginUrl(array(
            'scope' => 'publish_actions',
            'redirect_uri' => $current_url // Replace here
        ));
        die(header('Location: '. $login_url .''));
    }


    $res = $facebook->api('/me/feed', 'POST', array(
        'link' => '', // Replace and fill with your params
        'name' => '', // Replace and fill with your params
        'message' => '', // Replace and fill with your params
        'description' => '', // Replace and fill with your params
        'picture' => '', // Replace and fill with your params
    ));
} catch (FacebookApiException $e) {
    // 2.b Log any error and retry please

    die(header('Location: /'));
}

至于其余部分,请使用一个小表格发布到 facebook Controller 。如果您传递大量数据,请从 $_GET 中选择 $_POST。然后从 Controller 获取它们并填写我在代码中留下的 replace here 注释。如果您是 PHP 新手,则需要进一步的指导。在我看来,从 facebook SDK Controller 开始是错误的开始。好吧,我可能是错的。

关于php - 从 url/超链接发布 Open Graph Story,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658083/

相关文章:

php - 使用PHP在XML文件中搜索特定值

php - 如何在 Laravel 5 中连接到现有的 MySQL 数据库?

php - Laravel 上的图像更新并删除旧图像

facebook - Facebook 照片网址是否始终保持不变?

php - 是否可以将 Heroku Foreman 与 PHP 一起使用?

facebook - Internet Explorer 喜欢按钮疯狂

android - 必须使用 Activity 访问 token 来查询有关当前用户的信息

Facebook JS 连接不支持的浏览器 IE 手机

json - 为 Graph API 请求明确设置 "pretty print"格式

javascript facebook 检查用户是否是页面的粉丝