使用Facebook SDK在页面帖子/照片中标记persion

时间:2016-02-16 07:20:10

标签: php facebook api sdk

我可以编程方式代表页面发帖。我希望能够在帖子中发布,或标记,或者在评论中标记它们。我只需要创建一个帖子作为页面,并将其绑定到用户。

编辑:我真的希望能够发布一个页面,标记用户,或在评论中标记它们,或在照片中标记它们。任何形式的标记,通知他们。

我试过在照片中标记人物。标签显示为待处理,没有通知,当我批准标签时,它会永久消失。所以不太可能。

到目前为止,这是我的代码:

        <?php

    $pageId = 'removed';
    // Pass session data over.


    // Include the required dependencies.
    require( __DIR__.'/facebook/autoload.php' );

    session_start();

    //$_SESSION["accessToken"] = null;

    // Initialize the Facebook PHP SDK v5.
    $fb = new Facebook\Facebook([
      'app_id'                => 'removed',
      'app_secret'            => 'removed',
      'default_graph_version' => 'v2.5',
    ]);

    if(isset($_SESSION["accessToken"])){ //if authorized

        $photo = [
            'message' => 'Test message.',
            'source' => $fb->fileToUpload('glow.jpg'),
            'tags'    => array(
                 array(
                  'tag_uid'=> removed,
                  'tag_text' => 'name',
                  'x'      => '50',
                  'y'      => '50',
                 )
            )
        ];

        //auth page
        $response = $fb->get('/' . $pageId . '?fields=access_token', $_SESSION["accessToken"]);

        $pageNode = $response->getGraphNode();  
        $pageToken = $pageNode['access_token'];

        $response = $fb->post('/' . $pageId . '/photos', $photo, $pageToken);

        $postNode = $response->getGraphNode();  
        $postId = $postNode['id'];

        echo 'Page Token: ' . $pageToken . '<br />Post Id:' . $postId;

        $response = $fb->post('/' . $postId . '/comments', array (
            'message' => 'This is a test comment. @[{19292868552}:1:{Martin Firth}] tag thing.',
          ), $_SESSION["accessToken"]);

    } else { //not authorized yet
        $helper = $fb->getRedirectLoginHelper();

        $accessToken = $helper->getAccessToken();
        $_SESSION["accessToken"] = $accessToken;

        // Send the request to Graph
        try {
            $response = $fb->get('/' . $pageId . '?fields=access_token', $accessToken);
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
          // When validation fails or other local issues
            //echo 'Facebook SDK returned an error: ' . $e->getMessage();

            $permissions = ['email', 'user_likes', 'publish_pages', 'publish_actions'];
            $loginUrl = $helper->getLoginUrl('http://localhost/terranova/site/', $permissions);
            echo ' <a href="' . $loginUrl . '">Log in with Facebook!</a>';
          exit;
        }
    }

    ?>

0 个答案:

没有答案